0

当数据网格列是 ComboBox 时如何填充数据网格。在下面的代码中,我的列没有被填满......但是组合框包含一些项目。

示例代码。

<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>
4

1 回答 1

0
<ComboBox ItemsSource="..." SelectedItem="{Binding WHCode, Mode=TwoWay}" />

This will select the combobox-item (if found) that corresponds to the WHCode property of the current data-item. It will also allow for the combobox to update the model value as the user updates the selection.

I don't understand why you want to bind the combobox to the same collection as the datagrid. I would probably create a WHCodes property on the view-model, and use Path=DataContext.WHCodes (or something similar), to make it populate the combobox from a set of "WH Codes".

于 2012-04-10T17:04:04.773 回答