如何绑定 WPF DataGrid 的 DataGridComboBoxColumn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42791297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:02:32  来源:igfitidea点击:

How to bind a DataGridComboBoxColumn of a WPF DataGrid

c#wpfmvvmcomboboxdatagrid

提问by Patrick Pirzer

My project uses MVVM and I want to bind a DataGridComboBoxColumnto the viewmodel.

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

The combobox shall have the items "<" (with key "1") and "<=" (with key "2").

组合框应具有项目“<”(键为“1”)和“<=”(键为“2”)。

First i have an observablecollectionwith the comboboxitems:

首先,我有一个observablecollection组合框项目:

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

This is the class ArithmeticSignData:

这是 ArithmeticSignData 类:

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

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

When my viewmodel is initialized i fill the list LowerComparerItems:

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

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

The data for the datagrid comes from another observablecollection with an entity framework table as type. That table has a column called "low_operator". So i thought it would be possible to bind the comboboxcolumn by the following. When i open the combobox i can see the items. But after starting the app the values from the table are not translated to "<" or "<=".

数据网格的数据来自另一个以实体框架表为类型的 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>

采纳答案by mm8

You should the SelectedValueBindingproperty to your binding and also set the SelectedValuePathproperty to "ArithmeticSignKey":

您应该将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">

This should set the low_operatorcolumn to the value of the selected ArithmeticSignKeyvalue. If you want to set it to the ArithmeticSignValueyou should set the SelectedValuePathproperty of the column to the name of this one instead.

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

回答by Kadi Okba abdelmoumen

Edit Your code like this :

像这样编辑您的代码:

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

回答by AQuirky

Instead of SelectedItemBinding use this...

而不是 SelectedItemBinding 使用这个...

SelectedValueBinding="{Binding low_operator}"