WPF RadioButton Example (Checked)Use the WPF RadioButton control. Handle the RadioButton_Checked event.
RadioButton. A RadioButton offers an option. It is always visible, and is part of a group of other RadioButtons. One option can be "checked" at a time by the user. We use a RadioButton in WPF, handling its Checked event.
please create a WPF application and drag two RadioButton controls to the window. Next, add the "Checked" attribute to each "RadioButton" element in the XAML. Have Visual Studio create RadioButton_Checked.
Checked: The Checked event is triggered when the user clicks or selects a RadioButton. The "check" is a black circle.
Note: We can have both RadioButton elements point to a single RadioButton_Checked method. Two different methods could instead be used.
we access the RadioButton object that raised the event. We cast the "sender" object to a RadioButton type with the as-cast. We then call ToString on its Content property and change the Window Title.AsProperty
Example markup: XAML
Title="MainWindow" Height="350" Width="525">
Example code: C#
/// Interaction logic for MainWindow.xaml
public partial class MainWindow : Window
private void RadioButton_Checked(object sender, RoutedEventArgs e)
// ... Get RadioButton reference.
var button = sender as RadioButton;
// ... Display button content as title.
this.Title = button.Content.ToString();
How can you keep track of which RadioButton is currently checked? One way involves uses a field. Store a field (int, string) on the MainWindow class. And in Checked, assign it to the result of the Content.ToString() method.
Then: You can access this field anywhere (in any method) in your WPF program to determine which RadioButton is checked.
A CheckBox control may be checked at the same time as another. But only one RadioButton control can be checked. For this reason, a RadioButton can be used to select an exclusive option, such as a program mode.CheckBox
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.