2

I have a dropdown control placed on a winform. Today the datasource for this dropdown is KeyValuePair<'string','string'> and hence it is easy to directly assign the 'DisplayMember' property of the dropdown to 'Value' from the KeyValuePair.

One of my requirements has caused this datasource to be changed to KeyValuePair<'string', KeyValuePair<'string','string'>. This poses problems for me when I run my code. Thats because 'DisplayMember' being set to 'Value' causes the dropdown item to be shown as ('AB') (where 'A' and 'B' are the respective strings in KeyValuePair<'string','string'> of the new datasource).

I want to assign the 'DisplayMember' property of the dropdown to the 'Key' in the KeyValuePair<'string','string'> from the changed datasource.

Old requirement -

KeyValuePair<'string','string'>('A','B')

Dropdown item shows - 'B'

New requirement -

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

Dropdown item should show - 'B'

Is it possible to do implement with only change in dropdowns properties?

Have checked the datasource but it only shows me the top level of Key-Value pair and not the hierarchical model.

halfer
  • 19,824
  • 17
  • 99
  • 186
anand
  • 21
  • 4
  • Are these KeyValuePairs coming from a list? Can you modify the list before it becomes a DataSource or are you stuck with a DataSource that is in this format? – thinklarge Jul 08 '15 at 19:45
  • - Are these KeyValuePairs coming from a list? These KeyValuePairs are being created at runtime. I am stuck with the DataSource which is of the format KeyValuePair>. And more importantly, I cant give up the KeyValuePair type for the dropdown. :( – anand Jul 08 '15 at 20:39

2 Answers2

1

Unfortunately, you can't bind to nested properties using DisplayMember. So, trying to set DisplayMember to something like "Value.Key" doesn't work. However, I would suggest making a custom type that wraps your KeyValuePair<string, KeyValuePair<string, string>>> type into one object and give it properties that can be easily accessed. Here's an example:

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;
    }
}
Russ Wilson
  • 110
  • 10
  • I am already trying to make use of the NameValuePair which is provided by the CSLA framework. Any suggestions on this front ? I would not want to move out of the CSLA framework as I am using some default operation provided by CSLA. – anand Jul 08 '15 at 20:51
  • Unfortunately, I don't have experience with CLSA or the NameValuePair class, but my quick research tells me that it's just a non-generic class. However, you would STILL need a wrapper, given the fact that the "Value" property of the NameValuePair object would be a nested property. – Russ Wilson Jul 08 '15 at 21:06
0

Try the following:

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'))
Elton Santana
  • 950
  • 3
  • 11
  • 22