У меня есть элемент управления WPF ListView, для которого я динамически создаю столбцы.Один из столбцов оказывается столбцом CheckBox.Когда пользователь непосредственно нажимает на CheckBox, SelectedItem ListView не изменяется.Если бы флажок был объявлен в XAML, я бы добавил обработку для события Click, чтобы вручную установить выбор.Однако я озадачен, поскольку это динамический столбец.
<ListView
SelectionMode="Single"
ItemsSource="{Binding Documents}"
View="{Binding Converter={local:DocumentToGridViewConverter}}" />
Преобразователь принимает объект, с которым связаны свойства, существует пара имя / значение, на которую можно ссылаться через индексаторы.
public class DocumentToGridViewConverter : MarkupExtension, IValueConverter
{
private static DocumentToGridViewConverter mConverter;
public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
{
GridView gridView = null;
Document document = value as Document;
if( document != null )
{
// Create a new grid view.
gridView = new GridView();
// Add an isSelected checkbox complete with binding.
var checkBox = new FrameworkElementFactory( typeof( CheckBox ) );
gridView.Columns.Add( new GridViewColumn
{
Header = string.Empty, // Blank header
CellTemplate = new DataTemplate { VisualTree = checkBox },
} );
// Add the rest of the columns from the document properties.
for( int index = 0; index < document.PropertyNames.Length; index++ )
{
gridView.Columns.Add( new GridViewColumn
{
Header = document.PropertyNames[index];
DisplayMemberBinding = new Binding(
string.Format( "PropertyValues[{0}]", index ) )
} );
}
}
return gridView;
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
public override object ProvideValue( IServiceProvider serviceProvider )
{
if( mConverter == null )
{
mConverter = new DocumentToGridViewConverter();
}
return mConverter;
}
}
Итак, мой вопрос заключается в том, как мне создать чекбокс, который будет вызывать выбор строки ListView, когда пользователь нажимает на чекбокс.
Спасибо! РЕДАКТИРОВАТЬ:
Этот вопрос похож, но не имеет динамический кусок: WPF ListView SelectedItem является нулевым