Question

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?

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top