tags:

views:

56

answers:

4

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.

A: 

If you are putting strings into the combobox then you can use

MyComboBox.Items.IndexOf("Pizza")
rediVider
All this returns is -1 no matter what I set inside of the IndexOf
Mitchell Skurnik
Are you adding Strings to the ComboBox? If you do MyComboBox.Items[0], what type of object do you get back?
KeithMahoney
@KeithMahney System.Windows.Controls.ComboBoxItem
Mitchell Skurnik
As 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).
rediVider
@rediVider is 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
A: 

I see, you can add a name to the xaml

<ComboBoxItem Tag="0" Name="CBIPizza" IsSelected="True" Content="Pizza" />

then use

MyComboBox.Items.IndexOf(CBIPizza);

or... Make the items strings instead using

  <ComboBox Name="MyComboBox>
    <ComboBox.Items>
            <sys:String>Pizza</sys:String>
            <sys:String>Bread Sticks</sys:String>
      </ComboBox.Items>

which of course requires defining

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Then the original example would work

rediVider
+3  A: 

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.

Stephan
@Stephan this works perfectly. Thank you. I have marked this as the answer.
Mitchell Skurnik
The only modification I did was to add .ToString() at the end of the .Content to compare string values
Mitchell Skurnik
+1 Nice and Simple.
Avatar
+1  A: 

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();
    }
Avatar
Avatar your code would have worked but Stephan's was simpler
Mitchell Skurnik
Yeah. It was simple and clear. Thanks :) Great day :)
Avatar