在wpf datagrid中DataGridComboboxColumn的代码隐藏绑定

时间:2013-11-16 06:48:41

标签: c# wpf datagrid

如何在Code中执行此绑定:

<Grid >
    <Grid.Resources>
        <ObjectDataProvider x:Key="ProductDataProvider" ObjectType="{x:Type local:clsPurchaseOrderList}" MethodName="GetProducts" />
    </Grid.Resources>

我的数据网格为:

<my:DataGrid Name="dgvPurchaseOrder"
                             ItemsSource="{Binding}" 
                             SelectionUnit="CellOrRowHeader"
                             TabIndex="3">
                    <my:DataGrid.Columns>

                        <my:DataGridComboBoxColumn 
                                       Width="100"
                                       Header="Product Code"
                                       SelectedValueBinding="{Binding Path=Product_Id,UpdateSourceTrigger=PropertyChanged}"                                                                                       
                                       SelectedValuePath="Product_Id"
                                       DisplayMemberPath="Product_Code"                                           
                                       ItemsSource="{Binding Source={StaticResource ProductDataProvider}}">
                            <my:DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="ComboBox">
                                    <Setter Property="IsEditable" Value="True" />
                                </Style>
                            </my:DataGridComboBoxColumn.EditingElementStyle>
                        </my:DataGridComboBoxColumn>
                                   .
                                   .
                                   .
                    </my:DataGrid.Columns>
                </my:DataGrid>
</Grid>

我想在codebehind中绑定datagridComboboxColumn如何实现这个

1 个答案:

答案 0 :(得分:1)

尝试类似:

((DataGridComboBoxColumn)PaymentDistributionDataGrid.Columns[1]).ItemsSource = taskGetMortgageInterfaceInformation.Result.TransactionCodes.Where( x => !x.Description.Equals("@"));
                   ((DataGridComboBoxColumn)PaymentDistributionDataGrid.Columns[1]).SelectedValuePath = "Code";
                   ((DataGridComboBoxColumn)PaymentDistributionDataGrid.Columns[1]).SelectedValueBinding = new Binding("MITransactionCode");
                   ((DataGridComboBoxColumn)PaymentDistributionDataGrid.Columns[1]).DisplayMemberPath = "Code";   

PaymentDistribution是我的可编辑数据网格。