C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
Example markup: XAML
<Window x:Class="WpfApplication16.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<RadioButton Content="Dogs"
Checked="RadioButton_Checked"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"/>
<RadioButton Content="Cats"
Checked="RadioButton_Checked"
HorizontalAlignment="Left"
Margin="10,30,0,0"
VerticalAlignment="Top"/>
</Grid>
</Window>
Example code: C#
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication16
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
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();
}
}
}
Then: You can access this field anywhere (in any method) in your WPF program to determine which RadioButton is checked.