问题描述
我有一个DataGrid,它有一个DataGridTemplateColumn,它使用DataGrid的ItemsSource绑定,但是在DataGridTemplateColumn的ComboBox中,我想要能够绑定ViewView,而不是ItemsSource。 < DataGrid ItemsSource ={Binding ModelValues,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}>
< DataGridTemplateColumn Header =myHeaderWidth =200>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< ComboBox DisplayMemberPath =ValueSelectedValuePath =KeyIsEnabled =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 =ValueSelectedValuePath =KeyIsEnabled =False
SelectedValue ={Binding myID,Mode = TwoWay}
ItemsSource ={Binding Path = myList ,
RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type local:ViewModel}}}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
< / DataGrid>
ViewModel还有一个 ModelValues
属性作为 myList
属性。 ModelValues
用于 DataGrid
的 ItemsSource
,我想为 ComboBox
ItemsSource
使用 myList
/ p>
如何更改我的 RelativeSource
命令,以使其工作?
绑定到网格的数据文本:
< DataGrid ItemsSource ={Binding ModelValues,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}>
< DataGridTemplateColumn Header =myHeaderWidth =200>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< ComboBox DisplayMemberPath =ValueSelectedValuePath =KeyIsEnabled =False
SelectedValue ={绑定myID,Mode = TwoWay}
ItemsSource ={Binding Path = DataContext .myList,
RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type DataGrid}}}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< ComboBox DisplayMemberPath =ValueSelectedValuePath =KeyIsEnabled =False
SelectedValue ={Binding myID,Mode = TwoWay}
ItemsSource ={Binding Path = DataContext .myList,
RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type DataGrid}}}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
< / DataGrid>
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>
这篇关于如何使用RelativeSource Binding创建DataGrid绑定到Model和ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!