[英]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.