Reputation: 2371
I thought this would be easy, but now I don´t know how to do it exactly. In a WPF-application, i go from one window to another by clicking a contextmenu-item. My constructor for the new window looks like this:
public Bearbeitung(int loginid, String art)
On the Window, there´s a checkbox filled with a list. What I want is, that the default selected item in my checkbox is art
.
Ok, I checked if the String is in the list, but now I don´t know how to set it to the selecteditem in the combobox.
How can I manage this?
EDIT: I already tried
combobox.SelectedItem = art;
...that doesn´t work!
EDIT2:
Here´s the code:
List<String> feld = new List<string>();
feld = agrep.GetFelder(loginid);
foreach (String s in feld)
{
cbFeld.Items.Add(s);
}
if (cbFeld.Items.Contains(art))
{
MessageBox.Show("It contains it");
cbFeld.SelectedItem = art;
}
The messagebox isn´t shown!
Upvotes: 0
Views: 6348
Reputation: 2371
Ok, I just solve it. The problem was, that when I give the string to the other window, a blank has been added. Thanks to you all!
Upvotes: 0
Reputation: 185553
If your ComboBox
only contains strings you should be able to just set the SelectedItem
cb.SelectedItem = art;
If it does not only contain strings you might want to change that, e.g.
cb.ItemsSource = new string[] { "Item 1", "Item 2" };
If you have complex objects you will want to set the SelectedValue
and SelectedValuePath
instead.
Upvotes: 0
Reputation: 172478
If the list items are just strings, you can simply do
myComboBox.SelectedItem = art;
Upvotes: 2