首页
学习
活动
专区
圈层
工具
发布
50 篇文章
1
【愚公系列】2023年09月 WPF控件专题 XAML介绍
2
【愚公系列】2023年09月 WPF控件专题 WPF应用程序组成
3
【愚公系列】2023年09月 WPF控件专题 Window窗体属性和事件
4
【愚公系列】2023年09月 WPF控件专题 Label、TextBox、PasswordBox控件介绍
5
【愚公系列】2023年09月 WPF控件专题 Button控件详解
6
【愚公系列】2023年09月 WPF控件专题 RadioButton控件详解
7
【愚公系列】2023年09月 WPF控件专题 CheckBox控件详解
8
【愚公系列】2023年09月 WPF控件专题 Image控件详解
9
【愚公系列】2023年09月 WPF控件专题 Border控件详解
10
【愚公系列】2023年09月 WPF控件专题 ComboBox控件详解
11
【愚公系列】2023年09月 WPF控件专题 ListBox控件详解
12
【愚公系列】2023年09月 WPF控件专题 DatePicker控件详解
13
【愚公系列】2023年09月 WPF控件专题 Calendar控件详解
14
【愚公系列】2023年09月 WPF控件专题 Slider控件详解
15
【愚公系列】2023年09月 WPF控件专题 ProgressBar控件详解
16
【愚公系列】2023年10月 WPF控件专题 StackPanel控件详解
17
【愚公系列】2023年10月 WPF控件专题 WrapPanel控件详解
18
【愚公系列】2023年10月 WPF控件专题 DockPanel控件详解
19
【愚公系列】2023年10月 WPF控件专题 Canvas控件详解
20
【愚公系列】2023年10月 WPF控件专题 Grid控件详解
21
【愚公系列】2023年10月 WPF控件专题 Groupbox控件详解
22
【愚公系列】2023年10月 WPF控件专题 Expander控件详解
23
【愚公系列】2023年10月 WPF控件专题 TabControl控件详解
24
【愚公系列】2023年10月 WPF控件专题 Frame控件详解
25
【愚公系列】2023年10月 WPF控件专题 ListView控件详解
26
【愚公系列】2023年10月 WPF控件专题 DataGrid控件详解
27
【愚公系列】2023年10月 WPF控件专题 Menu控件详解
28
【愚公系列】2023年10月 WPF控件专题 ContextMenu控件详解
29
【愚公系列】2023年10月 WPF控件专题 TreeView控件详解
30
【愚公系列】2023年10月 WPF控件专题 ToolBar控件详解
31
【愚公系列】2023年10月 WPF控件专题 ToolBarTray控件详解
32
【愚公系列】2023年10月 WPF控件专题 StatusBar控件详解
33
【愚公系列】2023年11月 WPF控件专题 MediaElement控件详解
34
【愚公系列】2023年11月 WPF控件专题 RichTextBox控件详解
35
【愚公系列】2023年11月 WPF控件专题 GridView控件详解
36
【愚公系列】2023年11月 WPF控件专题 Rectangle控件详解
37
【愚公系列】2023年11月 WPF控件专题 Popup控件详解
38
【愚公系列】2023年11月 WPF控件专题 OpenFileDialog控件详解
39
【愚公系列】2023年11月 WPF控件专题 SaveFileDialog控件详解
40
【愚公系列】2023年11月 WPF控件专题 RepeatButton控件详解
41
【愚公系列】2023年11月 WPF控件专题 Ellipse控件详解
42
【愚公系列】2023年11月 WPF控件专题 Polygon控件详解
43
【愚公系列】2023年11月 WPF控件专题 Path控件详解
44
【愚公系列】2023年11月 WPF控件专题 WindowFormsHost控件详解
45
【愚公系列】2023年11月 WPF控件专题 WebBrowser控件详解
46
【愚公系列】2023年11月 WPF控件专题 Validation控件详解
47
【愚公系列】2023年11月 WPF控件专题 Page控件详解
48
【愚公系列】2023年11月 WPF控件专题 PrintDialog控件详解
49
【愚公系列】2023年11月 WPF控件专题 Track控件详解
50
【愚公系列】2023年11月 WPF控件专题 Polyline控件详解

【愚公系列】2023年09月 WPF控件专题 RadioButton控件详解

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏

🚀前言

WPF控件是Windows Presentation Foundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。

原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见的标准用户界面元素。

自定义控件则允许开发人员使用XAML和C#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。

🚀一、RadioButton控件详解

RadioButton是WPF中的一种控件,它允许用户从一组选项中选择一个选项。RadioButton通常与其他控件一起使用,如GroupBox或ListBox,以便在同一窗口或页面上显示多个选项。

以下是使用WPF中RadioButton的步骤:

  1. 在XAML中添加一个RadioButton控件。例如:
代码语言:html
复制
<RadioButton Content="Option 1" />
  1. 在同一容器中添加其他RadioButton控件以创建选项组。
代码语言:html
复制
<StackPanel>
   <RadioButton Content="Option 1" />
   <RadioButton Content="Option 2" />
   <RadioButton Content="Option 3" />
</StackPanel>
  1. 将RadioButton的GroupName属性设置为相同的值,以便它们被视为同一选项组。
代码语言:html
复制
<StackPanel>
   <RadioButton Content="Option 1" GroupName="Options" />
   <RadioButton Content="Option 2" GroupName="Options" />
   <RadioButton Content="Option 3" GroupName="Options" />
</StackPanel>
  1. 使用IsChecked属性检查哪个RadioButton被选中,并执行相应的操作。
代码语言:html
复制
<StackPanel>
   <RadioButton Content="Option 1" GroupName="Options" IsChecked="True" />
   <RadioButton Content="Option 2" GroupName="Options" />
   <RadioButton Content="Option 3" GroupName="Options" />
</StackPanel>

在这个例子中,第一个RadioButton被选中,并且可以在后台代码中使用IsChecked属性检查是否选中了该选项。

🔎1.属性介绍

RadioButton(单选按钮)是WPF中常用的控件之一,它可以与其他RadioButton控件进行分组,使得在同一组内只有一个控件可以被选中。以下是RadioButton的常用属性介绍:

  1. IsChecked:指示控件是否被选中的布尔值属性。
  2. Content:控件显示的内容,可以是文本、图像等内容。
  3. GroupName:将多个RadioButton控件分组,以便在同一组内只有一个控件可以被选中。分组的方式是将不同控件的GroupName属性设置为相同的字符串。
  4. CommandParameter:将控件与命令相关联时指定传递给命令的参数。
  5. FontWeight:控件中显示内容的字体宽度属性。
  6. FontSize:控件中显示内容的字体大小属性。
  7. Height:控件的高度属性。
  8. Width:控件的宽度属性。
  9. Margin:控件与其周围元素的间距属性。
  10. HorizontalAlignment:控件相对于水平方向的对齐方式属性。
  11. VerticalAlignment:控件相对于垂直方向的对齐方式属性。
  12. IsEnabled:指示控件是否可用的布尔值属性。
  13. ToolTip:控件的工具提示属性,显示鼠标悬停在控件上时的提示内容。
  14. Style:控件的样式属性,可以使用样式定义控件的外观和行为。

🔎2.常用场景

  1. 选项卡控件:在WPF中,我们可以通过RadioButton来实现选项卡控件,在选项卡中选择不同的RadioButton,来展示不同的内容页面。
  2. 单选列表:在WPF中,我们可以利用RadioButton来实现单选列表,用户只能选择其中一个选项。
  3. 问卷调查:在问卷调查中,我们通常会针对一个问题提供多个选项,利用RadioButton可以实现单选。
  4. 设置界面:在设置界面中,我们可以利用RadioButton来提供多个选项,同时只能选择其中一个。
  5. 数据筛选:在数据筛选的过程中,我们可以利用RadioButton来实现单选,在筛选条件中选择一个选项。

RadioButton可以用于任何需要单选功能的场景,让用户只能从多个选项中选择一个。

🔎3.具体案例

代码语言:c#
复制
<Grid Name="grid1">
    <RadioButton Content="管理员" GroupName="role" HorizontalAlignment="Left" IsChecked="True" Margin="43,101,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
    <RadioButton Content="学生"  GroupName="role" HorizontalAlignment="Left" Margin="115,101,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
    <RadioButton Content="教师" GroupName="role" HorizontalAlignment="Left" Margin="221,101,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
</Grid>
代码语言:c#
复制
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    RadioButton rbtn = new RadioButton();
    rbtn.Content = "主任";
    rbtn.GroupName = "role";
    rbtn.IsChecked = false;
    rbtn.HorizontalAlignment = HorizontalAlignment.Left;
    rbtn.VerticalAlignment = VerticalAlignment.Top;
    rbtn.Margin = new Thickness(320, 101, 0, 0);
   // rbtn.Checked += RadioButton_Checked;
   this.grid1.Children.Add(rbtn);
}

private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
    MessageBox.Show((sender as RadioButton).Content.ToString());
}

我正在参与2023腾讯技术创作特训营第二期有奖征文,瓜分万元奖池和键盘手表

下一篇
举报
领券