문제

I have a DataGrid which has a DataGridTemplateColumn that uses the ItemsSource binding of the DataGrid, but in the ComboBox of the DataGridTemplateColumn, I want to be able to bind to the ViewModel for the View instead of the ItemsSource.

 <DataGrid ItemsSource="{Binding ModelValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
      <DataGridTemplateColumn Header="myHeader" Width="200">
           <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                     <ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
                          SelectedValue="{Binding myID, Mode=TwoWay}"
                          ItemsSource="{Binding Path=myList, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}}" />
                </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
           <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                     <ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
                          SelectedValue="{Binding myID, Mode=TwoWay}"
                          ItemsSource="{Binding Path=myList, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}}" />
                </DataTemplate>
           </DataGridTemplateColumn.CellEditingTemplate>
      </DataGridTemplateColumn>
 </DataGrid>

The ViewModel has a ModelValues property as well as myList property. The ModelValues is used for the ItemsSource of the DataGrid and I want to use myList for the ComboBox ItemsSource.

How would I change my RelativeSource command so that it would work?

도움이 되었습니까?

해결책

Bind to the datacontext of the grid :

<DataGrid ItemsSource="{Binding ModelValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
      <DataGridTemplateColumn Header="myHeader" Width="200">
           <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                     <ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
                          SelectedValue="{Binding myID, Mode=TwoWay}"
                          ItemsSource="{Binding Path=DataContext.myList, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
                </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
           <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                     <ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" IsEnabled="False"
                          SelectedValue="{Binding myID, Mode=TwoWay}"
                          ItemsSource="{Binding Path=DataContext.myList, 
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
                </DataTemplate>
           </DataGridTemplateColumn.CellEditingTemplate>
      </DataGridTemplateColumn>
 </DataGrid>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top