2013-10-03 2 views
0

DataGrid에서 열거 형을 사용하려고합니다. 그것이 작동 할 수있는 두 가지 방법을 보여 드리겠습니다. 먼저 DataGrid 내에 ComboBox를 만들고 DataContext.MyOptions는 열거 형의 모든 값에 대한 문자열 목록을 반환합니다.ViewModel 속성을 DataGridComboBoxColum에 바인딩하십시오.

<DataGridTemplateColumn Header="Enum1"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <ComboBox ItemsSource="{Binding DataContext.MyOptions, RelativeSource={RelativeSource AncestorType=Window}}" 
         SelectedItem="{Binding Enum1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

다음 나는 내가 ElementStyle 및 EditingElementStyle 지금 내 질문에, 왜 수행한다

<DataGridComboBoxColumn Header="Enum1" Width="*" 
         SelectedItemBinding="{Binding Enum1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}"> 
    <DataGridComboBoxColumn.ElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=DataContext.MyOptions, RelativeSource={RelativeSource AncestorType=Window}}" /> 
     </Style> 
    </DataGridComboBoxColumn.ElementStyle> 
    <DataGridComboBoxColumn.EditingElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=DataContext.MyOptions, RelativeSource={RelativeSource AncestorType=Window}}" /> 
     </Style> 
    </DataGridComboBoxColumn.EditingElementStyle> 

</DataGridComboBoxColumn> 

(I 어딘가에서 복사)을 추가해야하는 DataGridComboBoxColumn을 사용하려고하지만,이 작업을 얻을 수 아래는 작동하지 않습니다. 열은 비어 있지만 값은 표시됩니다. 나는 다음과 같은 오류가 출력 창에서

<DataGridComboBoxColumn Header="Enum1" Width="*" 
         ItemsSource="{Binding Path=DataContext.MyOptions, RelativeSource={RelativeSource AncestorType=Window}}" 
         SelectedItemBinding="{Binding Enum1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}"> 
</DataGridComboBoxColumn> 

:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.MyOptions; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=59316889); target property is 'ItemsSource' (type 'IEnumerable') 

답변

2

DataGrid Columns dont come under the visualtree of their parent. 그 이유는 부모로부터 오는 they cannot inherit the DataContext도 아니고 조상을 참조 할 수도 있습니다.

DataGrid 행 및 셀은 다른 한편으로는 visualtree 아래에 있으므로 조상을 찾고 DataContext를 상속받습니다.

열을 바인딩하려면 use the BindingProxy이 필요합니다.

그것을 위해 빠른 답변

<DataGridComboBoxColumn Header="Enum1" Width="*" 
         ItemsSource="{Binding Path=Data.MyOptions, Source={StaticResource ProxyElement}" 
         SelectedItemBinding="{Binding Enum1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}"> 
</DataGridComboBoxColumn> 
+0

감사와 같은 열에 바인딩이 요소를 사용하여 다음

public class BindingProxy : Freezable { #region Overrides of Freezable protected override Freezable CreateInstanceCore() { return new BindingProxy(); } #endregion public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); } <DataGrid.Resources> <local:BindingProxy x:Key="ProxyElement" Data="{Binding}" /> </DataGrid.Resources> 

로 윈도우 자원에 하나 개의 자원을 정의하고 당신이 무엇을 할 수 있는지. 이 작업을 시도하면 다음과 같은 오류가 발생합니다. System.Windows.Data 오류 : 3 : DataContext를 제공하는 요소를 찾을 수 없습니다. BindingExpression : (경로 없음); DataItem = null; 대상 요소는 'FrameworkElement'(Name = ''); 대상 속성이 'DataContext'(유형 'Object') – uncletall

+0

내 잘못 .. 클래스가 Freezable 클래스를 확장해야한다고 말할 수 없습니다 .. 응답을 업데이트했습니다 ... Freezable은 DataContext가 상속되지 않은 데이터를 바인딩하는 데 사용됩니다 – Nitin

+0

! 방금 테스트를 거쳐 작동합니다! 아직도 많이 배울;) – uncletall

관련 문제