RadioButton
表示可由用户选择但不能清除的按钮。 可以通过单击来设置 IsChecked 的 RadioButton 属性,但只能以编程方式清除该属性。
RadioButton是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。
RadioButton具有两种状态:true
或 false
。 RadioButton是一个控件,该控件通常用作一组RadioButton控件中的项,也可以创建单个 RadioButton。RadioButton 的是被选中由其属性 IsChecked 的状态决定。
如果 RadioButton 被选中,则无法通过单击将其清除。 当 RadioButton 对元素进行分组时,按钮是互斥的。 用户在组中一次只能选择一个 RadioButton 项。 您可以通过将 RadioButton 控件放置在父控件内或通过设置每个RadioButton 控件的 GroupName 属性来对其进行分组。
若要对多个 RadioButton 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 RadioButton 的部分和状态,请参阅 单选按钮样式和模板。
此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题。
只有视觉对象属性已存在于控件的默认模板中并且已使用 TemplateBinding 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。
字段
名称 | 备注 | 权限 |
---|---|---|
标识 GroupName 依赖项属性 | public |
属性
名称 | 备注 | 权限 |
---|---|---|
获取或设置指定哪些 RadioButton 控件互相排斥的名称 | public |
方法
名称 | 备注 | 权限 |
---|---|---|
OnAccessKey | 在调用单选按钮的 AccessKey 时调用 | protected |
OnChecked | 在 IsChecked 属性变为 true 时调用 | protected |
OnCreateAutomationPeer | 提供 RadioButtonAutomationPeer 此控件的适当实现,作为 WPF 自动化基础结构的一部分 | protected |
OnToggle | 由 OnClick() 方法调用以实现 RadioButton 控件的切换行为。此方法将切换 IsChecked 属性。 | protected |
XAML代码
<Window x:Class="RadioButtonDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RadioButtonDemo"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="200" WindowStartupLocation="CenterScreen">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<RadioButton x:Name="Rb1" Content="Option 1" GroupName="MyGroup1" IsChecked="True" Checked="Rb1_Checked"/>
<RadioButton x:Name="Rb2" Content="Option 2" GroupName="MyGroup2" Checked="Rb2_Checked"/>
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RadioButtonDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Rb1_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString()+" is Checked");
}
private void Rb2_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString() + " is Checked");
}
private void Rb1_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString() + " is Unchecked");
}
private void Rb2_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString() + " is Unchecked");
}
}
}
此时GroupName不同,运行并点击Option2,如图
GroupName="MyGroup2" 改成 GroupName="MyGroup1",运行点击Option2,如图
Option1 和Option2同组互斥,点击 Option2,选中Option2,则自动取消Option1,引发Option 2的Checked,和Option 1的Unchecked,如图:
点击Option1,如下图
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RadioButtonDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Grid grid = (this as Window).Content as Grid;
StackPanel stackPanel = new StackPanel();
RadioButton radioButton1 = new RadioButton();
RadioButton radioButton2 = new RadioButton();
stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
stackPanel.VerticalAlignment = VerticalAlignment.Center;
radioButton1.Content = "Option 1";
radioButton1.GroupName = "MyGroup1";
radioButton1.IsChecked = true;
radioButton1.Checked += Rb1_Checked;
radioButton1.Unchecked += Rb1_Unchecked;
radioButton2.Content = "Option 2";
radioButton2.GroupName = "MyGroup1";
radioButton2.Checked += Rb2_Checked;
radioButton2.Unchecked += Rb2_Unchecked;
stackPanel.Children.Add(radioButton1);
stackPanel.Children.Add(radioButton2);
grid.Children.Add(stackPanel);
}
private void Rb1_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString()+" is Checked");
}
private void Rb2_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString() + " is Checked");
}
private void Rb1_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString() + " is Unchecked");
}
private void Rb2_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as RadioButton).Content.ToString() + " is Unchecked");
}
}
}