wpf 在组合框中显示字典 [key, value] 但在选择后只显示 [key]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12728379/
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 05:39:08  来源:igfitidea点击:

Show dictionary [key, value] in combobox but only [key] after selection

c#wpfcombobox

提问by user3357963

I have a dictionary which I want to allow the keys to be selected in a combobox but also display the value without having to create another column.

我有一个字典,我想允许在组合框中选择键,但也可以显示值而不必创建另一列。

For instance, if my dictionary consists of 3 items for now

例如,如果我的字典现在包含 3 个项目

Dictionary<string, double> test = new Dictionary<string, double>();
    //KEY , VALUE
    //"Item1" , 4.0
    //"Item2" , 4.5
    //"Item3" , 5.0

I want the combobox pulldown list to display the available selections showing BOTH key and value

我希望组合框下拉列表显示可用的选择,同时显示键和值

[Item1, 4.0]
[Item2, 4.5]
[Item3, 5.0]

and say if [Item2, 4.5]is selected then only the key is displayed in the combobox as the current selection, in this case the following would display after selection...

并说如果[Item2, 4.5]被选中,则只有键作为当前选择显示在组合框中,在这种情况下,选择后将显示以下内容...

Item2

Item2

I've tried the following but this shows [key,value]in both the combobox selection list and after selection

我尝试了以下操作,但这[key,value]在组合框选择列表和选择后都显示

comboboxColumn2.SelectedValuePath= "Key";
comboboxColumn2.ItemsSource = test;

I've also tried using combinations with comboboxColumn2.DisplayMemberPath= "Value";but cannot get it formatted as I would like.

我也尝试过使用与的组合,comboboxColumn2.DisplayMemberPath= "Value";但无法按照我的意愿对其进行格式化。

Can anyone say if it's possible and what the correct syntax is?

谁能说这是否可能以及正确的语法是什么?

EDIT:

编辑:

The combobox are actually ComboboxColumns so there are only 2 events available which don't appear very useful

组合框实际上是 ComboboxColumns,因此只有 2 个可用的事件看起来不是很有用

<DataGrid.Columns>
    <DataGridComboBoxColumn Header="Department Id" x:Name="comboboxColumn1"
            SelectedValueBinding="{Binding Department Id}" />
    <DataGridComboBoxColumn Header="Department Id" x:Name="comboboxColumn2"
        SelectedValueBinding="{Binding Department Name}"/>
</DataGrid.Columns>

回答by Vinicius

If you want to do this on code behind here it is :

如果你想在后面的代码上做到这一点,它是:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="168,100,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DropDownOpened="comboBox1_DropDownOpened" DropDownClosed="comboBox1_DropDownClosed" />

public MainPage()
    {
        InitializeComponent();
        var source = new Dictionary<string, double>();
        source.Add("Item1", 0.4);
        source.Add("Item2", 0.3);
        source.Add("Item3", 0.1);

        var formateDSource = new Dictionary<string, string>();

        foreach (var item in source)
        {
            formateDSource.Add(string.Format("[{0}, {1}]", item.Key, item.Value), item.Key);
        }

        comboBox1.ItemsSource = formateDSource;
        comboBox1.DisplayMemberPath = "Value";
    }

    private void comboBox1_DropDownOpened(object sender, EventArgs e)
    {
        comboBox1.DisplayMemberPath = "Key";
    }

    private void comboBox1_DropDownClosed(object sender, EventArgs e)
    {
        comboBox1.DisplayMemberPath = "Value";
    }

I just created an formatedSource and then changed the displayMemberPath according to your description.

我刚刚创建了一个 formatedSource,然后根据您的描述更改了 displayMemberPath。

Hope it helps

希望能帮助到你