DataGridTemplateColumn获取单元格的值

我正在使用WPF Toolkit中的WPF DataGrid

我在我的DataGrid添加了一个模板化列,每个单元格中都有一个CheckBox 。 现在我如何访问这些单元格中的值?

DataGrid其他列来自DataSet 。 我可以访问这些,但我无法获得我添加到DataGridDataGridTemplateColumn的值。

有人有主意吗?

你现在从视觉树中拉出东西。 那很辛苦,你找不到绑定因为它被埋没在单元格模板中。 我所做的是为这种东西添加我自己的列,该列派生自DataGridBoundColumn,这意味着它具有与所有其他所有类似的绑定:(我刚才写过它,它可能与某些人一起看)这让我们我只是使用直接绑定。 我不必设置单元格模板,我可以使用我更喜欢的DataTemplate。

  public class DataGridReadOnlyObjectDisplayColumn : DataGridBoundColumn { public DataGridReadOnlyObjectDisplayColumn() { //set as read only, this.IsReadOnly = true; } ///  /// Gets and Sets the Cell Template for this column ///  public DataTemplate CellTemplate { get { return (DataTemplate)GetValue(CellTemplateProperty); } set { SetValue(CellTemplateProperty, value); } } // Using a DependencyProperty as the backing store for CellTemplate. This enables animation, styling, binding, etc... public static readonly DependencyProperty CellTemplateProperty = DependencyProperty.Register("CellTemplate", typeof(DataTemplate), typeof(DataGridReadOnlyObjectDisplayColumn), new UIPropertyMetadata(null)); protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { //create the simple field text block ContentControl contentControl = new ContentControl(); contentControl.Focusable = false; //if we have a cell template use it if (this.CellTemplate != null) { contentControl.SetValue(ContentControl.ContentTemplateProperty, this.CellTemplate); } //set the binding ApplyBinding(contentControl, ContentPresenter.ContentProperty); //return the text block return contentControl; } ///  /// Assigns the Binding to the desired property on the target object. ///  internal void ApplyBinding(DependencyObject target, DependencyProperty property) { BindingBase binding = Binding; if (binding != null) { BindingOperations.SetBinding(target, property, binding); } else { BindingOperations.ClearBinding(target, property); } } protected override System.Windows.FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { //item never goes into edit mode it is a read only column return GenerateElement(cell, dataItem); } } 

现在,如果你可以到达列,你可以到达列上的绑定。 如果你可以进入单元格,那么你可以找到数据项(行数据)。 那么我所做的就是遵循绑定来获取单元格值。 这是非常低效的,而且是一个黑客。 但它的确有效。 遵循绑定我使用这个。

  private Object GetCellValue(Binding columnBinding, object dataSource) { Object valueField = null; if (columnBinding != null) { BindingEvaluator bindingEvaluator = new BindingEvaluator(); //copy the binding Binding binding = new Binding(); binding.Path = columnBinding.Path; binding.Source = dataSource; //apply the binding BindingOperations.SetBinding(bindingEvaluator, BindingEvaluator.BindingValueProperty, binding); //get the current cell item valueField = bindingEvaluator.BindingValue as IValueField; } return valueField; } 

最后一块是一个名为BindingEvaluator的辅助类,它有一个dp,用于跟踪绑定

  public class BindingEvaluator : DependencyObject { ///  /// Gets and Sets the binding value ///  public Object BindingValue { get { return (Object)GetValue(BindingValueProperty); } set { SetValue(BindingValueProperty, value); } } // Using a DependencyProperty as the backing store for BindingValue. This enables animation, styling, binding, etc... public static readonly DependencyProperty BindingValueProperty = DependencyProperty.Register("BindingValue", typeof(Object), typeof(BindingEvaluator), new UIPropertyMetadata(null)); } 

我称之为:

  var valueField = this.GetCellValue(column.Binding as Binding, datagrid.CurrentCell.Item);