DataGridTemplateColumn获得细胞的价值 [英] DataGridTemplateColumn get value of cell

查看:131
本文介绍了DataGridTemplateColumn获得细胞的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF 的DataGrid 从WPF工具包。

I'm using the WPF DataGrid from the WPF Toolkit.

我添加了一个模板列到我的的DataGrid ,它在每个单元都有一个复选框。现在我该怎样访问这些单元格内的数值?

I added a templated column to my DataGrid, which has a CheckBox in each cell. Now how do I access the values within these cells?

我在其他列的的DataGrid 来自的DataSet 。我可以访问这些,但我不能得到的值 DataGridTemplateColumn 我加入的DataGrid

My other columns in the DataGrid come from a DataSet. I can access these, but I cannot get to the values of the DataGridTemplateColumn I added to the DataGrid.

任何人有什么想法?

推荐答案

您到拉东西出来的视觉现在树。和多数民众努力工作,你无法找到绑定,因为埋在单元格模板。我所做的就是添加自己的列这种东西,列从DataGridBoundColumn派生的,这意味着它已经具有约束力的所有其他人一样:(我刚才写的,它很可能做一些看)这让我只是用一条直线的结合。 。我没有设定一个单元模板,我可以只使用一个DataTemplate,我更喜欢

your into pulling stuff out of the visual tree now. and thats hard work, you cant find the binding because that is buried in the cell template. what i did was add my own column for this kind of stuff, the column derives from DataGridBoundColumn, which means it has a binding like all the others: ( i wrote it a while ago, it could probably do with some looking at ) This lets me just use a straight binding. i dont have to set a cell template, i can just use a DataTemplate which i like better.

   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);
      }
   }

现在如果你能得到的列,你可以得到到该列的结合。如果你能得到的细胞,那么你可以找到数据项(行数据)。那么我要做的就是遵循结合来获取单元格的值。真的是低效的,这是一个黑客。但它的作品。遵循结合我用这个。

now if you can get to the column you can get to the binding on the column. if you can get to the cell then you can find the data item (the row data). then what i do is follow the binding to get the cell value. it is really inefficient, and it is a hack. but it works. to follow the binding i use this.

 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,即我使用遵循结合

and the last piece is a helper class called BindingEvaluator which has one dp, that i use to follow the binding

   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);

这篇关于DataGridTemplateColumn获得细胞的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆