2
我有一个DataGrid,它有一个DataGridTemplateColumn,它使用DataGrid的ItemsSource绑定,但是在DataGridTemplateColumn的ComboBox中,我希望能够绑定到视图的ViewModel而不是ItemsSource。如何使用RelativeSource绑定创建DataGrid绑定到Model和ViewModel?
<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>
视图模型具有ModelValues
财产所有权以及myList
财产。 ModelValues
用于DataGrid
的ItemsSource
,我想用myList
代替ComboBox
ItemsSource
。
我该如何更改我的RelativeSource
命令,以便它能正常工作?