2009-12-20 40 views
1

我正在使用WPF Toolkit中的WPF DataGridDataGridTemplateColumn獲取單元格的值

我在我的DataGrid中添加了一個模板列,它在每個單元中有一個CheckBox。現在我該如何訪問這些單元格中的值?

DataGrid我的其他專欄來自DataSet。我可以訪問這些,但我無法獲得DataGridTemplateColumn的值,我將其添加到DataGrid

任何人有任何想法?

回答

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

我您的解決方案存在問題。我想使用DataGridTemplateColumn。但是,正如你所建議的那樣,細胞的價值被埋在細胞模板之下。所以,你創建了你自己的控件直接使用DataTemplate。然後如果我嘗試使用您的模板,那麼當單元格正在編輯時如何獲得不同的控件,因爲您可能沒有CellEditingTemplate。 – Vishal 2014-07-12 20:48:11

+0

@Vishal對不起,我不記得,太久以前,我不再做WPF了。 – 2014-07-18 04:23:32