簡體   English   中英

Silverlight中的Combobox Selection Changed事件在更改列表項時顯示錯誤

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

我正在創建一個組合框事件,同時更改了已更改的選擇。 我的代碼是

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>

我得到一個窗口,如下所示

在此處輸入圖片說明

誰能幫我這個!!

您正在嘗試將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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM