如何获取选中radioButton的文本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取选中radioButton的文本相关的知识,希望对你有一定的参考价值。

比如
两个radioButton
一个boy
一个girl
点击其中某一个按钮
将其文本用MessageBox弹出
谢谢!

参考技术A private void Form1_Load(object sender, EventArgs e)

radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
//两个radiobutton绑定 选择时 触发的事件

private void radioButton1_CheckedChanged(object sender, EventArgs e)
//sender,默认代表当前控件所以通过转换就可以得出他的一系列属性以及方法和事件哦,很好很强大呀!!
if( ((RadioButton)sender).Checked )

MessageBox.Show(((RadioButton)sender).Text);

本回答被提问者采纳
参考技术B 太深奥,不了解, 参考技术C radioButton.Text不行么?

检查后如何防止RadioButton取消选中?

我有这样的enum

public enum DLTypeEnum
{
    Others = 1, People = 2, Company = 4,
}

我在我的ViewModel有一个属性,如下所示:

private DLTypeEnum _DLType;
public DLTypeEnum DLType
{
    get { return _DLType; }
    set { SetProperty(ref _DLType, value); }
}

这是我的xaml

<WrapPanel  Grid.Row="3" Grid.Column="1">
    <WrapPanel.Resources>
        <Converter:EnumToBoolConverter x:Key="EnumToBooleanConverter" />
    </WrapPanel.Resources>
    <RadioButton GroupName="DLType" IsChecked="{Binding Path=DLType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static dlAccount:DLTypeEnum.Others}}"/>
    <RadioButton GroupName="DLType" IsChecked="{Binding Path=DLType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static dlAccount:DLTypeEnum.People}}"/>
    <RadioButton GroupName="DLType" IsChecked="{Binding Path=DLType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static dlAccount:DLTypeEnum.Company}}"/>
 </WrapPanel>

而这个转换器:

 public class EnumToBoolConverter:IValueConverter
    {
        private int val;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int intParam = (int)parameter;
            val = (int)value;

            return ((intParam & val) != 0);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)//bool to enum
        {
            val ^= (int)parameter;
            return Enum.Parse(targetType, val.ToString());
        }
    }

一旦RadioButton被检查,我不希望它被取消选中,除非它选择其他RadioButton(即必须一直选择一个RadioButton)。

答案

试试这段代码:

<WrapPanel  Grid.Row="3" Grid.Column="1">
    <WrapPanel.Resources>
        <Converter:EnumToBoolConverter x:Key="EnumToBooleanConverter" />
    </WrapPanel.Resources>
    <RadioButton GroupName="DLType" IsChecked="{Binding Path=DLType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static dlAccount:DLTypeEnum.Others}}" Unchecked="RadioButton_Unchecked"/>
    <RadioButton GroupName="DLType" IsChecked="{Binding Path=DLType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static dlAccount:DLTypeEnum.People}}" Unchecked="RadioButton_Unchecked"/>
    <RadioButton GroupName="DLType" IsChecked="{Binding Path=DLType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static dlAccount:DLTypeEnum.Company}}" Unchecked="RadioButton_Unchecked"/>
 </WrapPanel>

和代码背后:

private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
{
    if((_viewModel.DLss.CurrentItem as DL).DLType == 0)
      ((RadioButton)sender).IsChecked = true;
}

以上是关于如何获取选中radioButton的文本的主要内容,如果未能解决你的问题,请参考以下文章

android 中如何获取radiogroup 中那个radiobutton被选择

如何获取radiobutton是不是选中

android 中如何获取radiogroup 中那个radiobutton被选择

如何判断哪个radiobutton被选中

java如何获取单选按钮RadioButton的值?

C#中怎样获取radiobutton的值