111,077
社区成员




<Window x:Class="RadioButttonUnCheckDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="95" WindowStyle="ToolWindow"
Title="Demo: Uncheckable radio buttons">
<Grid Height="24" Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" VerticalAlignment="Center">Please select: </Label>
<WrapPanel Grid.Column="1">
<RadioButton Name="radio1" Content="1" Margin="5,0,5,0" GroupName="GroupName1"
PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"
Checked="RadioButton_StateChanged"
Unchecked="RadioButton_StateChanged" />
<RadioButton Name="radio2" Content="2" Margin="5,0,5,0" GroupName="GroupName1"
PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"
Checked="RadioButton_StateChanged"
Unchecked="RadioButton_StateChanged" />
</WrapPanel>
<Label Grid.Column="2" VerticalAlignment="Center" Name="labelResult" />
</Grid>
</Window>
namespace RadioButttonUnCheckDemo
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public MainWindow()
{
this.InitializeComponent();
}
/// <summary>
/// Gets a value indicating whether RadioButton radio1 is checked.
/// </summary>
/// <value>
/// <c>true</c> if RadioButton radio1 is radio1 checked; otherwise, <c>false</c>.
/// </value>
private bool IsRadio1Checked
{
get { return this.radio1.IsChecked == null ? false : (bool)this.radio1.IsChecked; }
}
/// <summary>
/// Gets a value indicating whether RadioButton radio2 is checked.
/// </summary>
/// <value>
/// <c>true</c> if RadioButton radio2 is radio1 checked; otherwise, <c>false</c>.
/// </value>
private bool IsRadio2Checked
{
get { return this.radio2.IsChecked == null ? false : (bool)this.radio2.IsChecked; }
}
/// <summary>
/// Handles the PreviewMouseLeftButtonDown event of the RadioButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void RadioButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender != null && sender is RadioButton)
{
RadioButton radioButton = sender as RadioButton;
bool isChecked =
radioButton.IsChecked == null ?
false :
(bool)radioButton.IsChecked;
if (isChecked)
{
radioButton.IsChecked = false;
e.Handled = true;
}
}
}
/// <summary>
/// Handles the StateChanged event of the RadioButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private void RadioButton_StateChanged(object sender, RoutedEventArgs e)
{
this.SetResult();
}
/// <summary>
/// Sets the result.
/// </summary>
private void SetResult()
{
if (!this.IsRadio1Checked && !this.IsRadio2Checked)
{
this.labelResult.Content = "Selected: N/A";
}
else if (this.IsRadio1Checked)
{
this.labelResult.Content = "Selected: Radio 1";
}
else if (this.IsRadio2Checked)
{
this.labelResult.Content = "Selected: Radio 2";
}
}
}
}
public class RadioButtonEx : RadioButton
{
protected override void OnClick(EventArgs e)
{
if (this.Checked)
{
this.Checked = false;
}
else
{
base.OnClick(e);
}
}
}