C#-下拉列表的嵌套KeyValuePairs

C#-下拉列表的嵌套KeyValuePairs,c#,drop-down-menu,keyvaluepair,C#,Drop Down Menu,Keyvaluepair,我有一个放置在winform上的下拉控件。今天,此下拉列表的数据源是KeyValuePair,因此可以很容易地直接将下拉列表的“DisplayMember”属性从KeyValuePair分配给“Value” 我的一项要求导致此数据源更改为KeyValuePair,不幸的是,您无法使用DisplayMember绑定到嵌套属性。因此,试图将DisplayMember设置为类似“Value.Key”的值是行不通的。但是,我建议创建一个自定义类型,将KeyValuePair>类型包装到一个对象中,并为其

我有一个放置在winform上的下拉控件。今天,此下拉列表的数据源是KeyValuePair,因此可以很容易地直接将下拉列表的“DisplayMember”属性从KeyValuePair分配给“Value”


我的一项要求导致此数据源更改为KeyValuePair,不幸的是,您无法使用
DisplayMember
绑定到嵌套属性。因此,试图将
DisplayMember
设置为类似
“Value.Key”
的值是行不通的。但是,我建议创建一个自定义类型,将
KeyValuePair>
类型包装到一个对象中,并为其提供易于访问的属性。下面是一个例子:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // This was the old way 
        //List<KeyValuePair<string, KeyValuePair<string, string>>> myList = new List<KeyValuePair<string, KeyValuePair<string, string>>>();

        //myList.Add(new KeyValuePair<string, KeyValuePair<string, string>>("A", new KeyValuePair<string, string>("B", "C")));
        //myList.Add(new KeyValuePair<string, KeyValuePair<string, string>>("D", new KeyValuePair<string, string>("E", "F")));
        //myList.Add(new KeyValuePair<string, KeyValuePair<string, string>>("G", new KeyValuePair<string, string>("H", "I")));

        // This is the new way
        List<CustomKeyValuePairWrapper> myList = new List<CustomKeyValuePairWrapper>();
        myList.Add(new CustomKeyValuePairWrapper("A", new KeyValuePair<string, string>("B", "C")));
        myList.Add(new CustomKeyValuePairWrapper("D", new KeyValuePair<string, string>("E", "F")));
        myList.Add(new CustomKeyValuePairWrapper("G", new KeyValuePair<string, string>("H", "I")));

        comboBox1.DataSource = myList;
        comboBox1.DisplayMember = "ValueKey";
    }
}

public class CustomKeyValuePairWrapper
{

    public string Key { get; private set; }

    public KeyValuePair<string, string> Value { get; private set; }

    public string ValueKey
    {
        get { return Value.Key; }
    }

    public CustomKeyValuePairWrapper(string key, KeyValuePair<string, string> value)
    {
        Key = key;
        Value = value;
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
//这是老办法
//List myList=新列表();
//添加(新的KeyValuePair(“A”),新的KeyValuePair(“B”,“C”));
//添加(新的KeyValuePair(“D”),新的KeyValuePair(“E”,“F”));
//添加(新的KeyValuePair(“G”),新的KeyValuePair(“H”,“I”));
//这是新的方式
List myList=新列表();
添加(新的CustomKeyValuePairRapper(“A”),新的KeyValuePairRapper(“B”,“C”));
添加(新的CustomKeyValuePairRapper(“D”),新的KeyValuePairRapper(“E”,“F”));
添加(新的CustomKeyValuePairRapper(“G”),新的KeyValuePairRapper(“H”,“I”));
comboBox1.DataSource=myList;
comboBox1.DisplayMember=“ValueKey”;
}
}
公共类CustomKeyValuePairRapper
{
公共字符串密钥{get;private set;}
公钥值对值{get;private set;}
公共字符串ValueKey
{
获取{返回值.Key;}
}
public CustomKeyValuePairRapper(字符串键、KeyValuePairValue)
{
钥匙=钥匙;
价值=价值;
}
}
尝试以下操作:

public class CustomKeyValuePair
{
    public CustomKeyValuePair(string key, string value)
    {
        this.Key = key;
        this.Value = value;
    }
    public string Key { get; set; }
    public string Value { get; set; }
    public override string ToString()
    {
        return Key;
    }
}

KeyValuePair<'string',KeyValuePair<'string',CustomKeyValuePair>('A', new CustomKeyValuePair('B','C'))
公共类CustomKeyValuePair
{
public CustomKeyValuePair(字符串键、字符串值)
{
这个。键=键;
这个。值=值;
}
公共字符串密钥{get;set;}
公共字符串值{get;set;}
公共重写字符串ToString()
{
返回键;
}
}

KeyValuePai这些KeyValuePair来自列表吗?您能否在列表成为数据源之前对其进行修改,或者您是否一直使用这种格式的数据源?-这些KeyValuePairs是否来自列表?这些KeyValuePairs是在运行时创建的。我一直使用格式为KeyValuePair的数据源。更重要的是,我不能为下拉列表放弃KeyValuePair类型:(我已经在尝试使用CSLA框架提供的NameValuePair。在这方面有什么建议吗?我不想退出CSLA框架,因为我使用的是CSLA提供的一些默认操作。不幸的是,我没有使用CLSA或NameValuePair类的经验,但我的快速研究告诉我,这是非常有用的。)只是一个非泛型类。但是,考虑到NameValuePair对象的“Value”属性是嵌套属性,您仍然需要一个包装器。
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // This was the old way 
        //List<KeyValuePair<string, KeyValuePair<string, string>>> myList = new List<KeyValuePair<string, KeyValuePair<string, string>>>();

        //myList.Add(new KeyValuePair<string, KeyValuePair<string, string>>("A", new KeyValuePair<string, string>("B", "C")));
        //myList.Add(new KeyValuePair<string, KeyValuePair<string, string>>("D", new KeyValuePair<string, string>("E", "F")));
        //myList.Add(new KeyValuePair<string, KeyValuePair<string, string>>("G", new KeyValuePair<string, string>("H", "I")));

        // This is the new way
        List<CustomKeyValuePairWrapper> myList = new List<CustomKeyValuePairWrapper>();
        myList.Add(new CustomKeyValuePairWrapper("A", new KeyValuePair<string, string>("B", "C")));
        myList.Add(new CustomKeyValuePairWrapper("D", new KeyValuePair<string, string>("E", "F")));
        myList.Add(new CustomKeyValuePairWrapper("G", new KeyValuePair<string, string>("H", "I")));

        comboBox1.DataSource = myList;
        comboBox1.DisplayMember = "ValueKey";
    }
}

public class CustomKeyValuePairWrapper
{

    public string Key { get; private set; }

    public KeyValuePair<string, string> Value { get; private set; }

    public string ValueKey
    {
        get { return Value.Key; }
    }

    public CustomKeyValuePairWrapper(string key, KeyValuePair<string, string> value)
    {
        Key = key;
        Value = value;
    }
}
public class CustomKeyValuePair
{
    public CustomKeyValuePair(string key, string value)
    {
        this.Key = key;
        this.Value = value;
    }
    public string Key { get; set; }
    public string Value { get; set; }
    public override string ToString()
    {
        return Key;
    }
}

KeyValuePair<'string',KeyValuePair<'string',CustomKeyValuePair>('A', new CustomKeyValuePair('B','C'))