键值对组合框在WPF [英] Key Value Pair Combobox in WPF

查看:748
本文介绍了键值对组合框在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我键值对集合(防爆重点= MSFT值= MSFT微软),我绑定到ComboBox。 DisplayMemeberPath =价值。下面需要完成

  • 在选择一个项目只需要关键要显示的组合,

  • 用户也可以键入组合一个全新的价值。

我不能拿出一个支持这些功能的解决方案。解决了一个打破了其他。

 <组合框IsTextSearchEnabled =真NAME =cmbBrokersIsEditable =真
的ItemsSource ={结合经纪人codeS}SelectedValuePath =关键
 的DisplayMemberPath =值文本={结合SelectedBroker,模式=双向}>
 

解决方案

我想你要找的是如下:什么

 公共类ComboBoxPairs
{
    公共字符串_key {获得;组; }
    公共字符串_Value {获得;组; }

    公共ComboBoxPairs(字符串_key,串_value)
    {
        _key = _key;
        _Value = _value;
    }
}
 

然后你就可以和使用这个类像这样

 名单,其中,ComboBoxPairs> CBP =新的名单,其中,ComboBoxPairs>();

cbp.Add(新ComboBoxPairs(微软,MSFT));
cbp.Add(新ComboBoxPairs(苹果,AAPL));
 

和绑定到ComboBox你有

  cmbBrokers.DisplayMemberPath =_key;
cmbBrokers.SelectedValuePath =_Value;

cmbBrokers.ItemsSource = CBP;
 

,当你需要访问它只是做到这一点

  ComboBoxPairs CBP =(ComboBoxPairs)cmbBrokers.SelectedItem;

字符串_key = cbp._Key;
字符串_value = cbp._Value;
 

这是所有你需要做的。

Consider I have Key Value Pair Collection (Ex Key=MSFT Value=MSFT Microsoft) which I bind to the ComboBox. DisplayMemeberPath=Value. the Following needs to be accomplished

  • On Selection of a Item only the Key needs to be displayed in Combo,

  • the user could also type a brand new value in the Combo.

I cant come up with the solution that supports both these features. Solving one breaks the other.

<ComboBox IsTextSearchEnabled="True" Name="cmbBrokers" IsEditable="True" 
ItemsSource="{Binding BrokerCodes}" SelectedValuePath="Key" 
 DisplayMemberPath="Value" Text="{Binding SelectedBroker, Mode=TwoWay}">

解决方案

I guess what you're looking for is as follows.

public class ComboBoxPairs
{
    public string _Key { get; set; }
    public string _Value { get; set; }

    public ComboBoxPairs(string _key,string _value )
    {
        _Key = _key ;
        _Value = _value ;
    }
}

Then you go on and use this class like this

List<ComboBoxPairs> cbp = new List<ComboBoxPairs>();

cbp.Add(new ComboBoxPairs("Microsoft", "MSFT"));
cbp.Add(new ComboBoxPairs("Apple", "AAPL"));

And bind it to the combobox you have

cmbBrokers.DisplayMemberPath = "_Key";
cmbBrokers.SelectedValuePath = "_Value";

cmbBrokers.ItemsSource = cbp;

And When you need to access it just do this

ComboBoxPairs cbp = (ComboBoxPairs)cmbBrokers.SelectedItem;

string _key = cbp._Key;
string _value = cbp._Value;

This is all you need to do.

这篇关于键值对组合框在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆