2009-12-20 4 views
1

WPF Toolkit의 WPF DataGrid을 사용하고 있습니다.DataGridTemplateColumn 셀의 값을 얻습니다.

각 셀에 CheckBox이있는 DataGrid에 템플릿 기반 열을 추가했습니다. 이제이 셀 내의 값에 어떻게 액세스합니까?

DataGrid의 내 다른 열은 DataSet에서 가져옵니다. 나는 이것들에 접근 할 수 있지만 DataGrid에 추가 한 DataGridTemplateColumn의 값을 얻을 수 없다.

누구든지 아이디어가 있습니까?

답변

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

마지막 조각은 내가 바인딩

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

을 수행하는 데 사용하는, 하나의 DP를 가지고 BindingEvaluator라는 도우미 클래스이며, 그래서 같이 호출 :

var valueField = this.GetCellValue(column.Binding as Binding, datagrid.CurrentCell.Item); 
+0

I 귀하의 솔루션에 문제가 있습니다. DataGridTemplateColumn을 사용하려고합니다. 그러나 당신이 그 세포의 가치가 세포 주형 아래 묻혀 있다고 제안했듯이. 따라서 DataTemplate을 직접 사용하는 자체 컨트롤을 만들었습니다. 다음 템플릿을 사용하려고하면 CellEditingTemplate이없는 것처럼 셀이 편집 중일 때 어떻게 다른 컨트롤을 얻을 수 있습니까? – Vishal

+0

@Vishal 죄송합니다. 너무 오래 전 기억이 안납니다. WPF를 더 이상 사용하지 않습니다. –

관련 문제