C# 使ListBoxItems的行为类似于单选按钮
我在一个列表框内有一个ToggleButton,当单击该按钮时,我希望列表框中的所有其他项目都被取消选中 我正在尝试这个C# 使ListBoxItems的行为类似于单选按钮,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我在一个列表框内有一个ToggleButton,当单击该按钮时,我希望列表框中的所有其他项目都被取消选中 我正在尝试这个 private void ToggleButton_Click(object sender, RoutedEventArgs e) { spriteToggleButton _tb = sender as ToggleButton; for (int i = 0; i < aListBox.Items.Count; i+
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
spriteToggleButton _tb = sender as ToggleButton;
for (int i = 0; i < aListBox.Items.Count; i++)
{
ListBoxItem lbi = (ListBoxItem)aListBox.Items[i]; // invalid cast exception here
lbi.IsSelected = false;
}
_tb.IsChecked = true;
}
private void ToggleButton\u单击(对象发送器,路由目标)
{
spriteToggleButton _tb=发送方作为切换按钮;
对于(int i=0;i
但是我得到一个无效的强制转换异常
我本以为aListBox.Items[I]会返回一个ListBoxItem对象。使用
DataTemplate
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding SomeProperty}" GroupName="someName" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
由于所有的
单选按钮
都将具有相同的组名
,因此在任何时候都只会选中一个。我使用的是切换按钮而不是单选按钮。也许我应该换成那样。