How to use RelativeSource Binding to create DataGrid binding to Model and ViewModel?

Bob. picture Bob. · Dec 13, 2012 · Viewed 8.4k times · Source

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?

Answer

mathieu picture mathieu · Dec 13, 2012

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>