I need to put on my window 3 radio buttons and to make a user to chose only one button. I made a ListBox and set Selection mode = Single but i still can choose all of them , i need to wrap each item in something...i don't know what and how. Can anyone help? Maybe there is another way for presenting radio buttons and choosing only one...?
here is the 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">
&开发者_如何学运维lt;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>
Try the GroupName property on the RadioButton elements (see 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>
so in your case:
<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>
Give them(radio buttons) all a groupname that is same.
You need to give each RadioButton a GroupName
property, and have it be the same between buttons you wish to be mutually exclusive.
精彩评论