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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检查后如何防止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 radiobutton点击后一直是选中状态,怎么再点击后取消选中

qt怎么用代码让radiobutton取消选中

如何防止芯片被取消选中?

如何取消 RadioButton 或 CheckBox 选中的更改

android 代码中设置radiobutton选中与为选中颜色

让 WPF 的 RadioButton 支持再次点击取消选中的功能