【问题标题】:How to bind a DataGridComboBoxColumn of a WPF DataGrid如何绑定 WPF DataGrid 的 DataGridComboBoxColumn
【发布时间】:2017-08-05 02:27:03
【问题描述】:

我的项目使用 MVVM,我想将 DataGridComboBoxColumn 绑定到视图模型。

组合框应包含项目“

首先我有一个带有组合框项的observablecollection

public ObservableCollection<ArithmeticSignData> LowerComparerItems { get; set; }

这是 ArithmeticSignData 类:

public class ArithmeticSignData
{
    public ArithmeticSignData(string key, string value)
    {
        ArithmeticSignKey = key;
        ArithmeticSignValue = value;
    }

    public string ArithmeticSignKey { get; set; }
    public string ArithmeticSignValue { get; set; }
}

当我的视图模型被初始化时,我填写了 LowerComparerItems 列表:

private void FillLowerComparerItemsList()
{
    LowerComparerItems = new ObservableCollection<ArithmeticSignData>();
    LowerComparerItems.Add(new ArithmeticSignData("1", "<"));
    LowerComparerItems.Add(new ArithmeticSignData("2", "<="));
}

数据网格的数据来自另一个以实体框架表为类型的 observablecollection。 该表有一个名为“low_operator”的列。 所以我认为可以通过以下方式绑定组合框列。 当我打开组合框时,我可以看到这些项目。 但启动应用程序后,表中的值不会转换为“

<DataGridComboBoxColumn x:Name="cbc_LowerComparer"
                                    SelectedItemBinding="{Binding low_operator, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    DisplayMemberPath="ArithmeticSignValue"
                                    Header=" "
                                    Width="30">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.LowerComparerItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.LowerComparerItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
            <Setter Property="IsEditable" Value="True"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

【问题讨论】:

    标签: c# wpf mvvm combobox datagrid


    【解决方案1】:

    像这样编辑您的代码:

    <DataGridComboBoxColumn x:Name="cbc_LowerComparer"
                                    ItemsSource="{Binding LowerComparerItems, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedItemBinding="{Binding low_operator, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    DisplayMemberPath="ArithmeticSignValue"
                                    Header=" "
                                    Width="30">
    

    【讨论】:

      【解决方案2】:

      使用这个来代替 SelectedItemBinding...

      SelectedValueBinding="{Binding low_operator}"
      

      【讨论】:

        【解决方案3】:

        您应该将SelectedValueBinding 属性设置为您的绑定,并将SelectedValuePath 属性设置为“ArithmeticSignKey”:

        <DataGridComboBoxColumn x:Name="cbc_LowerComparer"
                                            SelectedValueBinding="{Binding low_operator, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            SelectedValuePath="ArithmeticSignKey"
                                            DisplayMemberPath="ArithmeticSignValue"
                                            Header=" "
                                            Width="30">
        

        这应该将low_operator 列设置为所选ArithmeticSignKey 值的值。如果要将其设置为ArithmeticSignValue,则应将列的SelectedValuePath 属性设置为该列的名称。

        【讨论】:

        • 非常感谢mm8! ComboBox 已经够难了,但在 DataGrid 中它们就更难了。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-21
        • 2014-01-04
        • 2017-06-25
        • 2014-02-28
        • 1970-01-01
        相关资源
        最近更新 更多