【问题标题】:How to programatically get the binding expression from a DataGridCheckBoxColumn如何以编程方式从 DataGridCheckBoxColumn 获取绑定表达式
【发布时间】:2020-09-13 23:02:09
【问题描述】:

给定一个 DataGridCheckBoxColumn 绑定到布尔对象的数据网格

<datagrid .....>
<DataGrid.Columns>
   <DataGridCheckBoxColumn  Header="Issues"  Binding="{Binding HasIssue,UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>

如何以编程方式获取绑定表达式以便能够调用 UpdateTarget()?

即。

   var expression = datagrid1.GetBindingExpression(DataGrid.**WhatProperty**);
    if (expression != null)
       expression .UpdateTarget();

我也试过了

var expression = BindingOperations.GetBindingExpression(datagrid1, ***`WhatDependencyObjectHere`***);

【问题讨论】:

  • 获取绑定表达式是什么意思? (在代码隐藏中)

标签: wpf data-binding datagrid


【解决方案1】:

要回答您的确切问题,您必须获得DataGridCheckBoxColumn 正在生成的实际CheckBox。这是一个示例函数。我不知道你的ItemsSource 是什么类型的集合,所以我打电话给我的TestObject 并将ItemsSource 设置为IList&lt;TestObject&gt;

static void UpdateBindingTarget(DataGrid dg, DataGridCheckBoxColumn col, TestObject item)
{
    DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(item);
    CheckBox cb = (CheckBox)col.GetCellContent(row);
    var be = cb.GetBindingExpression(CheckBox.IsCheckedProperty);
    if (be != null) { be.UpdateTarget(); }
}

真正的问题是为什么您首先要这样做。以上不是我认为的好做法,更多的是一种 hacky 解决方法。如果您需要绑定源来更新目标,它应该继承DependencyObject 并使用DependencyProperty,或者实现INotifyPropertyChanged 并引发PropertyChanged 事件。

【讨论】:

    猜你喜欢
    • 2011-05-08
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多