Harold Finch
Harold Finch

Reputation: 586

How to get the appropriate ComboBox Tag

I'm trying to get the Tag associated to a value of ComboBox like this way:

var league = ((ComboBoxItem)this.League.SelectedValue).Tag.ToString();
Console.WriteLine(league);

The compiler show me a Invalid Cast Exception

I only want to get the associated Tag of the selected value by user, in particular:

(combobox value and Tag)

-Italy (item) - 10 (Tag)
-France (item) - 12 (Tag)

If the user select Italy, in the code I must get "10". But I can't do this, what am I doing wrong?

UPDATE (Populate combo):

List<RootObject> obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);

        foreach (var item in obj)
        {
            foreach (var code in nation_code)
            {
                if (code.Equals(item.League))
                {
                    League.Items.Add(item.Caption);
                    //link for each team
                    League.Tag = item.Links.Teams.href;
                }
            }
        }

Upvotes: 1

Views: 1733

Answers (2)

Yogi
Yogi

Reputation: 9759

If you see the Tag is setting for the combo box itself and not for its individual item.

You can build a dictionary and use it as datasource of your combo box. Specify the value and display members of the combo box with dictionary key and value attributes

Try modifying the combo population logic as follows -

        List<RootObject> obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);
        Dictionary<string, string> comboSource = new Dictionary<string, string>();

        foreach (var item in obj)
        {
            foreach (var code in nation_code)
            {
                if (code.Equals(item.League))
                {
                    comboSource.Add(item.Caption, item.Links.Teams.href);

                }
            }
        }

        League.ValueMember = "Value";
        League.DisplayMember = "Key";
        League.DataSource = comboSource;

And then the required values can be fetched using the selectedText and selectedvalue properties.

     League.SelectedText; //Return the "item.Caption"
     League.SelectedValue; //Return the "item.Links.Teams.href" 

For WPF we need to use different properties viz. ItemsSource, DisplayMemberPath and SelectedValuePath while binding the combo box. The above solution is for win forms.

Upvotes: 1

LInsoDeTeh
LInsoDeTeh

Reputation: 1038

You can add any type of object to a ComboBox, it does not need to be a string, it just needs to overwrite .ToString().

You could define a class:

class League {
  public string Country { get; set; }
  public int Id { get; set; }

  public override string ToString() {
       return Country;
  }
}

and then just add these objects to the ComboBox:

comboBox.Items.Add(new League { Country = "France", Id = 10 });

You can then just cast the SelectedItem of your comboBox back to your class:

var selectedLeague = (League)comboBox.SelectedItem;
//access selectedLeague.Country;
//access selectedLeague.Id;

Upvotes: 0

Related Questions