Win10系列:C#应用控件基础6
RadioButton控件
在应用程序的开发过程中开发者经常使用多个RadioButton控件来显示一组单选按钮,仅允许用户从中选择一项。RadioButton控件和CheckBox控件的差别在于,用户可以一次选择多个CheckBox复选框,而RadioButton单选按钮却只能选择同组中的一个。
在XAML文件中,RadioButton控件的用法如下所示:
<RadioButton .../>
-或-
<RadioButton ...>
<!--添加内容-->
</RadioButton>
下面介绍一下RadioButton控件的几个常用属性:
- Content属性,获取或设置RadioButton控件的文本内容。
- GroupName属性,获取或设置哪些RadioButton控件互相排斥。通过为RadioButton控件指定GroupName属性,将GroupName属性值相同的多个RadioButton控件分为一组,同一组内的选项互相排斥,只能从这组中选择一项。
- IsChecked属性,获取或设置RadioButton控件是否被选中。当RadioButton控件被选中时,其IsChecked属性的值为true;否则RadioButton控件的IsChecked属性值为false。
- Name属性,获取或设置RadioButton控件的名称。
介绍完常用属性后,接着来看一下RadioButton控件的常用事件:
- Click事件,当单击RadioButton控件时触发。
- Checked事件,当选中RadioButton控件时触发。
- Unchecked事件,当选中的RadioButton控件被取消时触发。
接下来使用RadioButton控件设计一个选择性别的应用示例。
新建一个名为"RadioButtonDemo"的Windows应用商店的空白应用程序项目,在MainPage.xaml文件的Grid元素中添加如下代码。
<RadioButton GroupName="性别" Name="MaleRadioButton" Content="男" HorizontalAlignment="Left" Margin="697,245,0,0" VerticalAlignment="Top"/>
<RadioButton GroupName="性别" Name="FemaleRadioButton" Content="女" HorizontalAlignment="Left" Margin="761,245,0,0" VerticalAlignment="Top"/>
<TextBlock FontSize="20" Name="ShowSelected" HorizontalAlignment="Left" Margin="697,299,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="29" Width="120"/>
<Button Content="显示选择" HorizontalAlignment="Left" Margin="558,290,0,0" VerticalAlignment="Top" Click=" ShowSelected_Click"/>
<TextBlock HorizontalAlignment="Left" Margin="558,245,0,0" TextWrapping="Wrap" Text="选择性别:" FontSize="25" VerticalAlignment="Top" Height="37" Width="134"/>
在上面的代码中,添加了两个RadioButton控件分别用于显示"男"和"女"单选按钮,设置这两个RadioButton控件的GroupName属性值都为"性别"。再添加一个Button按钮和两个TextBlock文本块,设置Button按钮的Content属性值为"显示选择","显示选择"按钮用于触发事件以显示选中的单选按钮内容,两个TextBlock文本块分别用于显示文本"选择性别:"和选择的单选按钮内容文本信息。
现在可以运行程序查看界面效果,如图4-11所示。

图4-11 选择性别应用
在MainPage.xaml.cs文件中,为"显示选择"按钮的Click事件添加处理方法ShowSelected_Click,当单击"显示选择"按钮时,在ShowSelected文本块中显示选择的单选按钮内容,代码如下所示:
private void ShowSelected_Click(object sender, RoutedEventArgs e)
{
string selectedContent = null;
//判断选择的单选按钮是"男"还是"女"
if (MaleRadioButton.IsChecked == true)
{
selectedContent = "男";
}
if (FemaleRadioButton.IsChecked == true)
{
selectedContent = "女";
}
//判断是否选中了单选按钮
if (selectedContent != null)
{
ShowSelected.Text = "选择了" + selectedContent;
}
else
{
ShowSelected.Text = "还没有选择";
}
}
在上面的代码中,定义一个string类型的变量selectedContent并赋值为空,通过RadioButton控件的IsChecked属性判断"男"和"女"单选按钮的状态。当单选按钮被选中时,其IsChecked属性值为true,根据选中的单选按钮状态为selectedContent变量赋予不同的值。当selectedContent变量不为空时,将selectedContent变量显示在前台界面,否则显示提示信息"还没有选择"。
运行程序,选择"男"单选按钮,单击"显示选择"按钮,会在界面中显示选择结果"选择了男",效果如图4-12所示。

图4-12 选择性别为"男"后效果
Win10系列:C#应用控件基础6的更多相关文章
- Win10系列:JavaScript 控件的使用
向页面中添加的控件可分为两种类型:标准的HTML控件和WinJS库控件.其中标准的HTML控件是指HTML标准中定义的基本控件,如按钮和复选框:WinJS库控件是为开发基于JavaScript 的Wi ...
- WPF从我炫系列4---装饰控件的用法
这一节的讲解中,我将为大家介绍WPF装饰控件的用法,主要为大家讲解一下几个控件的用法. ScrollViewer滚动条控件 Border边框控件 ViewBox自由缩放控件 1. ScrollView ...
- WPF 模仿 UltraEdit 文件查看器系列一 用户控件
WPF 模仿 UltraEdit 文件查看器系列一 用户控件 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 章节: 起步 添加用户控件 ...
- C#控件系列--文本类控件
C#控件系列--文本类控件 文本类控件主要包含Label.LinkLabel.Button.TextBox以及RichTextBox. Label 功能 Label用来 ...
- 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree
原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...
- 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试
原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...
- WPF编游戏系列 之四 用户控件
原文:WPF编游戏系列 之四 用户控件 在上一篇<WPF编游戏系列 之三 物品清单>中,对物品清单进行了演示,其中反复用到了同一组控件(如下图),而且 颜昌钢也指出在3.2. ...
- Delphi XE2 之 FireMonkey 入门(44) - 控件基础: TTreeView、TTreeViewItem
Delphi XE2 之 FireMonkey 入门(44) - 控件基础: TTreeView.TTreeViewItem TScrollBox -> TCustomTreeView -> ...
- Delphi XE2 之 FireMonkey 入门(43) - 控件基础: TStringGrid、TGrid
Delphi XE2 之 FireMonkey 入门(43) - 控件基础: TStringGrid.TGrid TStringGrid.TGrid 都是从 TCustomGrid 继承; 区别有:1 ...
- Delphi XE2 之 FireMonkey 入门(42) - 控件基础: TComboBox、TComboEdit
Delphi XE2 之 FireMonkey 入门(42) - 控件基础: TComboBox.TComboEdit TListBox 有两个兄弟 TComboListBox.TComboEditL ...
随机推荐
- ionic3 在ios9.0 系统下 会出现ReferenceError:Can't find variable:Intl 错误提示
ionic3 框架开发app 在ios 9.0版本中 ReferenceError:Can't find variable:Intl 错误提示: 在index.html 文件中添加 <scri ...
- 新装Windows Server 2008 r2无法连接有线网络
新装的Windows Server 2008 r2没有网卡驱动,所以没有网络适配器. 首先,我在相同的型号电脑上查到这个主板的网卡驱动安装的是Intel(R) Ethernet Coinnection ...
- 记录tiny6410 jlink 命令行调试linux-2.6.38内核
1\首先启动nandflash uboot->linux内核->文件系统,进入文件系统命令行 2\启动JLinkGDBServer -device ARM11 3\启动arm-none-e ...
- mybatis源码解析11---ParameterHandler解析
ParameterHandler接口是参数处理器,位于mybatis包的org.apache.ibatis.executor.parameter下,源码如下: public interface Par ...
- websocket 二合一
server from flask import Flask, request, render_template from geventwebsocket.handler import WebSock ...
- WIFI CAT ET III Adapter Caterpillar ET3 New Arrival
The old bluetooth cat et adatper iii has stopped production, and you can get the new WIFI CAT Caterp ...
- 约定优于配置---Java的eclipse项目配置
0.测试文件夹test (测试文件的文件夹和源文件夹src是并行的关系,且位于同一目录) 以后源文件.java文件放在src目录下,相应的单元测试文件放在同级别的test目录下,且内部路径要相同 1. ...
- 【题解】JSOIWC2019 Round2
题面: 题解: T1: 毕竟是tg膜你,不会太难 就是一道简单贪心 首先,对于a<=b的所有物品,一定是贪心的按照a从小到大放入. 先假设剩下的物品可以按照某种顺序放进去,那么可以得到一个最终空 ...
- 腾讯云centos7.2安装jdk1.7 tomcat7.0部署项目示例
说实话win server的性能并不好,所以程序员必须会在Linux上安装环境,部署项目. 第一步,官网下载tomcat和jdk压缩文件*.tar.gz 下载路径如下: jdk:http://www ...
- C# HttpWebResponse WebClient 基础连接已经关闭: 发送时发生错误.
https://blog.csdn.net/sun49842566/article/details/82802297 net 4.0 设置: ServicePointManager.SecurityP ...