Set Silverlight combobox selected item from string name
Solution 1
If you have a reason that you have to wrap the strings in ComboBoxItem
then this should work.
MyComboBox.Items.SelectedItem =
MyComboBox.Items.SingleOrDefault(c => (c as ComboBoxItem).Content == myString);
I would recommend to not directly insert ComboBoxItem
and set items to String
or setup a collection in code and bind to it.
Solution 2
Hi i am applied function for encountred index in Combobox
private int Search_Item_Return_Index(ComboBox combo, string Search)
{
int index=-1;
foreach (ComboBoxItem item in combo.Items)
{
index++;
string var = item.Content.ToString() ;
if (var.Equals(Search))
{
return index;
}
}
return index;
}
Solution 3
You can achieve this by using the following.
SetSelectedItem("Pizza");
/// Set selected item as string.
private void SetSelectedItem(string selectedString)
{
Func<ComboBoxItem, ComboBoxItem> selectionFunc = (item) =>
{
if(item.Content.ToString() == selectedString)
return item;
return null;
};
this.MyComboBox.SelectedItem = MyComboBox.Items.Select(s => selectionFunc(s as ComboBoxItem)).FirstOrDefault();
}
Comments
-
Mitchell Skurnik almost 2 years
I am trying to set the selected item in my silverlight combobox from a string.
In this example lets say I have a combobox named "MyComboBox" as follows:
<ComboBox Height="23" x:Name="MyComboBox" Width="200" HorizontalAlignment="Left"> <ComboBoxItem Tag="0" Content="Pizza" IsSelected="True"/> <ComboBoxItem Tag="1" Content="Soda"/> <ComboBoxItem Tag="2" Content="Wings"/> <ComboBoxItem Tag="3" Content="Bread Sticks"/> </ComboBox>
I am randomly selecting a string value above from a list to simulate a users saved preference. The problem I am facing is trying to grab the index of "MyComboBox" from a string.
I've tried using MyComboBox.items wtih LINQ but that has taken me nowhere.
There are some similar questions here on stack overflow but none of these have been answered.
-
Mitchell Skurnik over 13 yearsAll this returns is -1 no matter what I set inside of the IndexOf
-
KeithMahoney over 13 yearsAre you adding Strings to the ComboBox? If you do MyComboBox.Items[0], what type of object do you get back?
-
Mitchell Skurnik over 13 years@KeithMahney System.Windows.Controls.ComboBoxItem
-
rediVider over 13 yearsAs I said, this works if you are adding strings to the combobox. If you set var myPizzaObject = new Food("Pizza"); MyComboBox.Items.Add(myPizzaObject); then, if its selected, the MyComboBox.SelectedItem is your myPizzaObject. To find it you would need to have a reference to myPizzaObject stored somewhere, and use MyComboBox.Items.IndexOf(myPizzaObject).
-
Mitchell Skurnik over 13 yearsis there any way to get the Index of if you are not adding strings to it but are defining the comboboxitems how I have it in my updated question/example?
-
Mitchell Skurnik over 13 yearsthis works perfectly. Thank you. I have marked this as the answer.
-
Mitchell Skurnik over 13 yearsThe only modification I did was to add .ToString() at the end of the .Content to compare string values