2015-02-06 39 views
0

我有一个数据网格DataGridTemplateColumn包含CheckBox。我创建了列如下:WPF复选框滚动思考数据网格检查事件

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); 

现在我有问题,即滚动throught的DataGrid期间,CheckedEvent叫,有Checked == False

通风口这里下标:

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

如何滚动调用事件?

也许有人有一个想法,谢谢!

+1

只是一个建议。您不应该通过WPF中的代码来构建您的视图。改为使用XAML标记。 – dymanoid 2015-02-06 15:24:22

+0

为什么我不这么做?因为我觉得比使用XAML更适合c#:) – BendEg 2015-02-06 15:28:41

+0

嗯,你不必这么做。正如我所说,这只是一个建议。如果你知道如何使用它,XAML确实使事情变得更加容易,因为它更具可读性和声明性。 – dymanoid 2015-02-06 15:57:19

回答

1

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