本文介绍了如何使用RelativeSource Binding创建DataGrid绑定到Model和ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:42