Question

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!!

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top