DataGridTemplateColumn获取单元格的值

时间:2009-12-20 19:35:42

标签: c# wpf datagrid wpftoolkit datagridtemplatecolumn

我正在使用WPF工具包中的WPF DataGrid

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

DataGrid中的其他列来自DataSet。我可以访问这些内容,但无法获得DataGridTemplateColumn添加到DataGrid的{​​{1}}的值。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:3)

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

   public class DataGridReadOnlyObjectDisplayColumn : DataGridBoundColumn {

      public DataGridReadOnlyObjectDisplayColumn() {
         //set as read only,
         this.IsReadOnly = true;
      }


      /// <summary>
      /// Gets and Sets the Cell Template for this column
      /// </summary>
      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;
      }

      /// <summary>
      ///     Assigns the Binding to the desired property on the target object.
      /// </summary>
      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 {

      /// <summary>
      /// Gets and Sets the binding value
      /// </summary>
      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);