Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使ListBoxItems的行为类似于单选按钮_C#_Silverlight_Windows Phone 7 - Fatal编程技术网

C# 使ListBoxItems的行为类似于单选按钮

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+

我在一个列表框内有一个ToggleButton,当单击该按钮时,我希望列表框中的所有其他项目都被取消选中

我正在尝试这个

    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>


由于所有的
单选按钮
都将具有相同的
组名
,因此在任何时候都只会选中一个。

我使用的是切换按钮而不是单选按钮。也许我应该换成那样。