选择单个单选按钮,wpf

时间:2011-06-16 11:33:51

标签: c# .net wpf listbox radio-button

我需要在窗口上放置3个单选按钮,并让用户只选择一个按钮。 我做了一个ListBox并设置了Selection mode = Single但我仍然可以选择所有这些,我需要将每个项目包装成某些东西......我不知道是什么以及如何。有人可以帮忙吗?也许有另一种方式来呈现单选按钮并只选择一个......?

这是xaml -

<ListBox SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent" BorderThickness="0" Margin="0,0,0,57" HorizontalAlignment="Right" Width="304" Height="146" VerticalAlignment="Bottom">
        <ListBoxItem>
            <RadioButton Content="Option 1" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left"  Name="radioButton1" VerticalAlignment="Top" FontSize="12" />
        </ListBoxItem>
        <ListBoxItem>
            <RadioButton Content="Option 2" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left"  Name="radioButton2" VerticalAlignment="Top" FontSize="12" />
        </ListBoxItem>
        <ListBoxItem>
            <StackPanel Orientation="Horizontal" Height="90">
                <RadioButton Content="Another : " Checked="radioButton4_Checked" Height="16" HorizontalAlignment="Left"  Name="radioButton4" VerticalAlignment="Top" FontSize="12" />
                <TextBox Width="225" Name="TextBox_AnotherReason" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"/>
            </StackPanel>
        </ListBoxItem>
    </ListBox> 

3 个答案:

答案 0 :(得分:9)

尝试RadioButton元素上的GroupName属性(参见http://arcanecode.com/2007/09/20/the-wpf-radiobutton/)!

<StackPanel>
  <RadioButton GroupName=“One“ IsChecked=“True“>Option 1</RadioButton>
  <RadioButton GroupName=“One“ IsChecked=“False“>Option 2</RadioButton>
  <RadioButton GroupName=“Two“ IsChecked=“False“>Option 3</RadioButton>
  <RadioButton GroupName=“Two“ IsChecked=“True“>Option 4</RadioButton>
</StackPanel>

所以在你的情况下:

<ListBox SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent" BorderThickness="0" Margin="0,0,0,57" HorizontalAlignment="Right" Width="304" Height="146" VerticalAlignment="Bottom">
    <ListBoxItem>
        <RadioButton GroupName=“Group1“ Content="Option 1" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left"  Name="radioButton1" VerticalAlignment="Top" FontSize="12" />
    </ListBoxItem>
    <ListBoxItem>
        <RadioButton GroupName=“Group1“ Content="Option 2" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left"  Name="radioButton2" VerticalAlignment="Top" FontSize="12" />
    </ListBoxItem>
    <ListBoxItem>
        <StackPanel Orientation="Horizontal" Height="90">
            <RadioButton GroupName=“Group1“ Content="Another : " Checked="radioButton4_Checked" Height="16" HorizontalAlignment="Left"  Name="radioButton4" VerticalAlignment="Top" FontSize="12" />
            <TextBox Width="225" Name="TextBox_AnotherReason" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"/>
        </StackPanel>
    </ListBoxItem>
</ListBox> 

答案 1 :(得分:2)

给他们(单选按钮)所有相同的组名。

答案 2 :(得分:2)

您需要为每个RadioButton提供GroupName属性,并且在您希望互相排斥的按钮之间保持相同。