locked
Simple comboBox to filter the message in Silverlight! RRS feed

  • Question

  • In my silverlight page, there are a comboBox named cboFilter. This comboBox has four items in the dropdownlist: 
    1. - Please select - 
    2. BMW
    3. Lexus
    4. Lincoln
    and textbox use to display the message.
    if I select item2 then the message is displayed : BMW is a German car;
    if I select item3 then the message is displayed : Lexus is a Japan car;
    if I select item 4 then the message is displayed :  Lincoln is a American car.
    
    
    Here are my codes
    1. MainPage.xaml
    
    
    <ComboBox Canvas.Left="409" Canvas.Top="25" Height="23" Name="cboFilter" Width="120" SelectionChanged="cboFilter_SelectionChanged">
    <ComboBoxItem Content="-Please select-" IsSelected="True" />
    <ComboBoxItem Content="Lexus" />
    <ComboBoxItem Content="BMW" />
    <ComboBoxItem Content="Lincoln" />
    </ComboBox>
    <TextBox Height="25" Name="txtFilter" Width="15" Canvas.Left="514" Canvas.Top="-8" Text="0" />
    
    
    2. MainPage.xaml.cs
    public MainPage()
    {
    InitializeComponent();
    }
    private void cboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    if (cboFilter.SelectedIndex == 1)
    txtFilter.Text = " BMW is a German car";
       else if (cboFilter.SelectedIndex == 2)
    txtFilter.Text = "Lexus is a Japan car";
       else if (cboFilter.SelectedIndex == 3)
    txtFilter.Text = "Lincoln is a American car";
    }
    I got an issue that first page load, it fired the cboFilter_SelectionChanged Event 
    and when I select each item in the dropdownlist that did not show the message. Anything wrong with my code? 
    Thanks for your time to look in my issues.
    Thursday, December 1, 2011 9:13 PM

Answers

  • Hello,

    I found the answer when removing all items in xaml page and adding items in the MainPage() code behind by using 

    cboFilter.Item.Add("-Please select-");

    cboFilter.Item.Add("BMW");

    cboFilter.Item.Add("Lexus");

    cboFilter.Item.Add("Lincoln");

    cboFilter.SelectedIndex = 0;

    then eveything is working perfectly. Thanks.

    Thursday, December 1, 2011 11:53 PM