【问题标题】:WPF CheckBox checked event on scrolling thought datagrid滚动思想数据网格上的 WPF CheckBox 检查事件
【发布时间】:2015-02-06 15:16:17
【问题描述】:

我有一个带有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));

滚动怎么调用事件?

也许有人有想法,谢谢!

【问题讨论】:

  • 只是一个建议。您不应该通过 WPF 中的代码来构建视图。请改用 XAML 标记。
  • 为什么我不这样做?因为我觉得 C# 比 XAML 更舒服 :)
  • 好吧,你不必这样做。正如我所说,这只是一个建议。如果您知道如何使用 XAML,它确实会让事情变得更容易,因为它更具可读性和声明性。

标签: c# wpf events checkbox datagrid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2018-10-01
    • 2017-09-08
    • 2016-07-11
    • 1970-01-01
    相关资源
    最近更新 更多