从代码绑定DataGridComboBoxColumn

时间:2011-08-03 16:44:12

标签: .net wpf datagrid

我在datagrid wpf

中有这些列
        <DataGrid.Columns>
            <DataGridComboBoxColumn SelectedItemBinding="{Binding Path=ProductId}" DisplayMemberPath="Name" SelectedValuePath="Id" />         
            <DataGridTextColumn Binding="{Binding Path=StorageAmount}" Width="*" Header="Со склада" />
            <DataGridTextColumn Binding="{Binding Path=Trade1Amount}" Width="*" Header="С точки 1" />
            <DataGridTextColumn Binding="{Binding Path=Trade2Amount}" Width="*" Header="С точки 2" />
            <DataGridTextColumn Binding="{Binding Path=TotalAmount}" Width="*" Header="Всего" FontWeight="Bold" />
        </DataGrid.Columns>

如何以编程方式从代码绑定 DataGridComboBoxColumn ,而不是从xaml绑定?我无法将 Name 定义为 DataGridComboBoxColumn ,因此我无法从代码访问以填充此Combobox的 DataContext

1 个答案:

答案 0 :(得分:1)

将x:-namespace与Name-attribute一起使用。这将做你想要的:

<DataGridComboBoxColumn x:Name="m_yourComboBoxColumn" SelectedItemBinding="{Binding Path=ProductId}" DisplayMemberPath="Name" SelectedValuePath="Id" />