基于MVVM模式的XAML数据绑定
当datagrid列为ComboBox时,如何填充datagrid。 在下面的代码中,我的列没有被填满……但是组合框包含了一些项目 示例代码基于MVVM模式的XAML数据绑定,xaml,Xaml,当datagrid列为ComboBox时,如何填充datagrid。 在下面的代码中,我的列没有被填满……但是组合框包含了一些项目 示例代码 <Window x:Class="ComboBox_Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window x:Class="ComboBox_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding First}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="177,60,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200">
<DataGrid.Columns>
<DataGridTemplateColumn Header="WH Code" Width="70">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Height="22" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Last}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>` </DataGrid.Columns>
</DataGrid>
</Grid>
`
这将选择与当前数据项的WHCode
属性相对应的组合框项(如果找到)。它还允许combobox在用户更新选择时更新模型值
我不明白为什么要将combobox绑定到与datagrid相同的集合。我可能会在视图模型上创建一个WHCodes
属性,并使用Path=DataContext.WHCodes
(或类似的东西),使其从一组“WH-code”填充组合框
<ComboBox ItemsSource="..." SelectedItem="{Binding WHCode, Mode=TwoWay}" />