【问题标题】:DataGridTemplateColumn get value of cellDataGridTemplateColumn 获取单元格的值
【发布时间】:2010-12-28 13:09:01
【问题描述】:

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

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

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

有人有什么想法吗?

【问题讨论】:

    标签: c# wpf datagrid wpftoolkit datagridtemplatecolumn


    【解决方案1】:

    您现在可以从视觉树中提取内容。这很辛苦,你找不到绑定,因为它隐藏在单元格模板中。我所做的是为这种东西添加我自己的列,该列派生自 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);
    

    【讨论】:

    • 我对您的解决方案有疑问。我想使用 DataGridTemplateColumn。但是正如您所建议的那样,单元格的值被埋在单元格模板下。因此,您创建了自己的控件,该控件直接使用 DataTemplate。然后,如果我尝试使用您的模板,那么如何在单元格编辑时获得不同的控件,因为您可能没有 CellEditingTemplate。
    • @Vishal 对不起,我不记得了,太久以前了,我不再做 WPF。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多