WPF 如何判断哪个radioButton被选中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 如何判断哪个radioButton被选中相关的知识,希望对你有一定的参考价值。
参考技术A 前台xaml:<Grid>
<StackPanel>
<RadioButton x:Name="rbA" Content="选项A" GroupName="group1"></RadioButton>
<RadioButton x:Name="rbB" Content="选项B" GroupName="group1"></RadioButton>
</StackPanel>
</Grid>
后台:
放在构造函数中。
rbA.Checked += new RoutedEventHandler(radio_Checked);
rbB.Checked += new RoutedEventHandler(radio_Checked);
rbA.Unchecked += new RoutedEventHandler(radio_Unchecked);
rbB.Unchecked += new RoutedEventHandler(radio_Unchecked);
void radio_Unchecked(object sender, RoutedEventArgs e)
RadioButton btn = sender as RadioButton;
if (btn == null)
return;
if (btn.Name == "rbA")
if (btn.Name == "rbB")
void radio_Checked(object sender, RoutedEventArgs e)
// 同UnChecked判断。
WPF绑定Radiobutton到enum
WPF中经常会需要用到多个Radiobutton选择项,并且需要将选中的选项映射到某个枚举值enum中。
这种情形的解决办法如下:
1) 需要创建一个UserControl,举个例子代码如下:
<UserControl x:Class="ShangfeiXApp.XModeUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:src="clr-namespace:ShangfeiXApp"
mc:Ignorable="d">
<UserControl.Resources>
<src:XDataModeValueConverter x:Key="XmodeConverter"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
<RadioButton Content="Background" Margin="0 0 10 0" GroupName="Mode" IsChecked="{Binding mode, Mode=TwoWay, ConverterParameter=BACKGROUND_MODE, Converter={StaticResource XmodeConverter}}" />
<RadioButton Content="Full scale" Margin="0 0 10 0" GroupName="Mode" IsChecked="{Binding mode, Mode=TwoWay, ConverterParameter=FULLSCALE_MODE, Converter={StaticResource XmodeConverter}}" />
<RadioButton Content="Normal" Margin="0 0 10 0" GroupName="Mode" IsChecked="{Binding mode, Mode=TwoWay, ConverterParameter=NORMAL_MODE, Converter={StaticResource XmodeConverter}}" />
</StackPanel>
</Grid>
</UserControl>
2)定义枚举和一个携带枚举属性的类,如下:
public enum XDataMode { BACKGROUND_MODE, FULLSCALE_MODE, NORMAL_MODE };
public class XDataModeValue : INotifyPropertyChanged
{
private XDataMode _mode = XDataMode.BACKGROUND_MODE;
public XDataMode mode
{
get { return _mode; }
set
{
if (_mode != value)
{
_mode = value;
NotifyPropertyChanged("mode");
}
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
3)定义bool与该枚举值得转化类:
[ValueConversion(typeof(XDataMode), typeof(Boolean))]
public class XDataModeValueConverter : IValueConverter
{
// bool转Visibility
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value.ToString() == parameter.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Enum.Parse(targetType, parameter.ToString(), true) : null;
}
}
4)在mainwindows.cs中或其他代码中类似如下定义一个变量:
public XDataModeValue Xdatamodevalue { get; set; }
5)在MainWindow中实现如下源码,即给定上下文,便于绑定
public MainWindow()
{
InitializeComponent();
...
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
DataContext = Xdatamodevalue;
}
完毕!
以上是关于WPF 如何判断哪个radioButton被选中的主要内容,如果未能解决你的问题,请参考以下文章
android 中如何获取radiogroup 中那个radiobutton被选择