22-Windows10应用控件-单选按钮RadioButton
单选按钮控件是表示一个单项选择的按钮控件,使用单选按钮控件,用户可以从一组选项中选择一个选项。
语法
<RadioButton.../>Code language: HTML, XML (xml)
或者
<RadioButton>内容</RadioButton>Code language: HTML, XML (xml)
可以通过将 RadioButton 控件放到父控件或者为每个 RadioButton 设置 GroupName 来对 RadioButton 进行分组。当 RadioButton 分为一组后,按钮之间会互相排斥,也就是只能选中其中一项。
按钮的状态有选中或者未选中。通过 IsChecked 可以判断是否选中,为 true 则表示选中,false 表示未选中。
要清除 RadioButton 可以单击该组中另外一个 RadioButton,不能通过再次单击来清除。当然可以通过编程的方式来设置 RadioButton 的 IsChecked。
练一练
你喜欢那个手机品牌?
<RadioButton
x:Name="radioButton"
Content="篮球"
HorizontalAlignment="Left"
Margin="90,135,0,0"
VerticalAlignment="Top"
GroupName="Sport"/>
<RadioButton
x:Name="radioButton1"
Content="羽毛球"
HorizontalAlignment="Left"
Margin="90,172,0,0"
VerticalAlignment="Top"
GroupName="Sport"/>
<RadioButton
x:Name="radioButton2"
Content="网球"
HorizontalAlignment="Left"
Margin="90,209,0,0"
VerticalAlignment="Top"
GroupName="Sport"/>
<RadioButton
x:Name="radioButton3"
Content="诺基亚"
HorizontalAlignment="Left"
Margin="145,430,0,0"
VerticalAlignment="Top"
IsChecked="True"/>
<RadioButton
x:Name="radioButton4"
Content="苹果"
HorizontalAlignment="Left"
Margin="145,467,0,0"
VerticalAlignment="Top"/>
<RadioButton
x:Name="radioButton5"
Content="小米"
HorizontalAlignment="Left"
Margin="145,504,0,0"
VerticalAlignment="Top"
Checked="radioButton5_Checked"
Unchecked="radioButton5_Unchecked"/>
<Button
x:Name="button"
Content="Button"
HorizontalAlignment="Left"
Margin="145,340,0,0"
VerticalAlignment="Top"
Click="button_Click"/>Code language: HTML, XML (xml)
Previous: 超链接HyperlinkButton
Next: 复选框CheckBox