locked
Set combobox selecteditem (object) to value from string RRS feed

  • Question

  • Hi,

    I have a combobox with itemsource set to a wcf class called twostrings with two strings called string1 and string2. In my client code I retrieve a string with the value of string1. How to set selecteditem of the combobox to the matching string1 of the string value retrieved?

    BR /Magnus

    Sunday, September 15, 2013 7:12 PM

All replies

  • Hello Flowhill1,

    try to this code:

    var abc = combobox1..SelectedItem as twostrings;

    public class twostrings
    {
    public string string1{get;set;}
    public string string2{get;set;}
    }

    this would be helpful for you.

    Monday, September 16, 2013 4:47 AM
  • Thanks for your reply.

    The problem is that I already have the class as you show, and that I try to set the selecteditem, not retrieve it.

    Here's a complete code from a simple project I created for tests.

            <ComboBox x:Name="cmb1" HorizontalAlignment="Left" Margin="30" VerticalAlignment="Top" Width="423" Height="39">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding String1}" Grid.Row="0" FontSize="22" />
                            <TextBlock Text="{Binding String2}" Grid.Row="1" />
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" Margin="30,90" VerticalAlignment="Top" Click="btn1_Click"/>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                List<TwoStrings> lst = new List<TwoStrings>();
                TwoStrings ts1 = new TwoStrings();ts1.String1 = "US"; ts1.String2 = "United States"; lst.Add(ts1);
                TwoStrings ts2 = new TwoStrings(); ts2.String1 = "SE"; ts2.String2 = "Sweden"; lst.Add(ts2);
                TwoStrings ts3 = new TwoStrings(); ts3.String1 = "DK"; ts3.String2 = "Denmark"; lst.Add(ts3);
                cmb1.ItemsSource = lst;
            }
    
            private void btn1_Click(object sender, RoutedEventArgs e)
            {
                // As example I here recieve a value from a database as string SE (example). The object itself is not recieved, only the text.
                // Here I want to set the selected item to the combobox item with text SE.
            }
        }
        public class TwoStrings
        {
            public string String1 { get; set; }
            public string String2 { get; set; }
        }
    Best regards /Magnus
    Monday, September 16, 2013 1:28 PM