Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于MVVM模式的XAML数据绑定_Xaml - Fatal编程技术网

基于MVVM模式的XAML数据绑定

基于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"

当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"
    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}" />