Combobox Selection Changed event in silverlight shows error while changing the listitem

StackOverflow https://stackoverflow.com/questions/23086494

  •  04-07-2023
  •  | 
  •  

Вопрос

I am creating a combobox event while changing the selection changed is done. My code is

C#

private void smscbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (((ComboBox)sender).SelectedItem != null)
    {
        ComboBoxItem cbi = (ComboBoxItem)((ComboBox)sender).SelectedItem;
        if (cbi.Content.ToString() == "Selected Class")
        {
            selectedclass.Visibility = Visibility.Visible;
            dgstudentsms.Visibility = Visibility.Visible;
        }
        else
        {
            selectedclass.Visibility = Visibility.Collapsed;
            dgstudentsms.Visibility = Visibility.Collapsed;
        }
    }
}    

and in XAML

<ComboBox Name="sendsmscbox" SelectionChanged="smscbox_SelectionChanged" >
    <ListBoxItem Content="All Students"/>
    <ListBoxItem Content="Selected Students"/>
</ComboBox>

I am getting a window as shown below

enter image description here

can any one help me with this!!

Это было полезно?

Решение

You're trying to cast a ListBoxItem to a ComboBoxItem.

Use ComboBoxItem in your XAML markup instead of ListBoxItem.

<ComboBox Name="sendsmscbox" SelectionChanged="smscbox_SelectionChanged" >
    <ComboBoxItem Content="All Students"/>
    <ComboBoxItem Content="Selected Students"/>
</ComboBox>

Or if you really meant to use a ListBoxItem in your XAML (probably not), then cast to the correct type in your code-behind:

ListBoxItem lbi = (ListBoxItem)((ComboBox)sender).SelectedItem;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top