WPF CheckBox在滚动思想数据网格上检查了事件

时间:2015-02-06 15:16:18

标签: c# wpf events checkbox datagrid

我有一个数据网格,其中DataGridTemplateColumn包含CheckBox。我创建了这样的列:

DataGridTemplateColumn cTemplateColumn = new DataGridTemplateColumn();
cTemplateColumn.Header = "Auswahl";
FrameworkElementFactory cFactory = new FrameworkElementFactory(typeof(CheckBox));
Binding b1 = new Binding("[__intern_cv__]");
//b1.IsAsync = true;
b1.Converter = new StringToBoolConverter(this);
b1.Mode = BindingMode.TwoWay;
b1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
cFactory.SetValue(CheckBox.IsCheckedProperty, b1);
cFactory.SetValue(CheckBox.HorizontalAlignmentProperty, HorizontalAlignment.Center);
cFactory.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(CheckedEvent));
cFactory.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(CheckedEvent));
cFactory.AddHandler(CheckBox.PreviewMouseDownEvent, new MouseButtonEventHandler(checkBoxMouseDown));
DataTemplate cCellTemplate = new DataTemplate();
cCellTemplate.VisualTree = cFactory;
cTemplateColumn.CellTemplate = cCellTemplate;    
Columns.Add(cTemplateColumn);

现在我遇到了问题,在DataGrid滚动期间,CheckedEvent被调用Checked == False

这里的通风口是:

cFactory.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(CheckedEvent));
cFactory.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(CheckedEvent));

滚动如何调用事件?

也许有人有个主意,谢谢!

1 个答案:

答案 0 :(得分:1)

默认情况下,DataGrid虚拟化其行。这意味着在滚动期间,旧行会重新使用新数据。如果新数据的"[__intern_cv__]"设置为false,而之前的数据设置为true,则会引发Unchecked事件。