2

我正在使用DataGridWPF 工具包中的 WPF。

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

我的其他专栏DataGrid来自DataSet. 我可以访问这些,但我无法获得DataGridTemplateColumn我添加到DataGrid.

有人有想法么?

4

1 回答 1

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);
于 2009-12-20T22:46:55.283 回答