组合框选择Silverlight中的更改事件在更改listitem时显示错误

时间:2014-04-15 14:20:10

标签: c# silverlight xaml combobox

我正在创建一个组合框事件,同时更改选择更改已完成。我的代码是

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;
        }
    }
}    

XAML

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

我正在看一个如下所示的窗口

enter image description here

任何人都可以帮助我!!

1 个答案:

答案 0 :(得分:1)

您正在尝试将ListBoxItem投射到ComboBoxItem

在您的XAML标记中使用ComboBoxItem而不是ListBoxItem

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

或者,如果您真的打算在XAML 中使用ListBoxItem(可能不是),那么在代码隐藏中转换为正确的类型:

ListBoxItem lbi = (ListBoxItem)((ComboBox)sender).SelectedItem;