尝试更改 WPF 中绑定的 RadioButton 时的奇怪行为

Odd behavior when trying to change a bound RadioButton in WPF(尝试更改 WPF 中绑定的 RadioButton 时的奇怪行为)

本文介绍了尝试更改 WPF 中绑定的 RadioButton 时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 Child 窗口中的两个单选按钮绑定到在主窗口中构造的 ViewModel 中的 Enum.绑定按预期工作,但我注意到一个我无法解决的非常奇怪的行为.我已经在此处提供了所有代码,因此您可以自己轻松地重构问题.

I've bound two radio buttons in my Child window to an Enum in my ViewModel which is constructed in the Main window. The binding works as expected but I have noticed a very odd behavior which I can't solve. I have provided all the code here so you can reconstruct the problem easily for yourself.

以下是查看这种奇怪行为的步骤:

Here are the steps to see this odd behavior:

  1. 点击主窗口中的按钮
  2. ChildWindow 打开并且 RadioButton 设置为 User
  3. 选择自动,然后再次选择用户
  4. 关闭 ChildWindow 并重新打开它!尝试将 RadioButton 更改为 Automatic.它不会改变!
  1. Click on the button in the MainWindow
  2. The ChildWindow opens and the RadioButton is set to User
  3. Choose Automatic and then Choose User again
  4. Close the ChildWindow and reopen it again! Try to change the RadioButton to Automatic. It won't change!

    <Window x:Class="RadioButtonBinding.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="TWFpbldpbmRvdw==" Height="350" Width="525">

        <Button Content="Display Child Window" Click="DisplayChildWindow"/> 
    </Window>

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            var viewModel = new ViewModel();
            DataContext = viewModel;
        }

        private void DisplayChildWindow(object sender, RoutedEventArgs e)
        {
            var win = new ChildWindow {DataContext = (ViewModel) DataContext};
            win.ShowDialog();
        }
    }

    <Window x:Class="RadioButtonBinding.ChildWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:radioButtonBinding="clr-namespace:RadioButtonBinding"
            Title="Q2hpbGRXaW5kb3c=" Height="300" Width="300">
        <Window.Resources>
            <radioButtonBinding:EnumBooleanConverter x:Key="EnumBooleanConverter"/>
        </Window.Resources>

        <StackPanel>
            <RadioButton Content="Automatic" 
                         GroupName="CalcMode"
                         IsChecked="{Binding Path=CalcMode, 
                                             Converter={StaticResource EnumBooleanConverter}, 
                                             ConverterParameter={x:Static radioButtonBinding:CalcMode.Automatic}}"/>

            <RadioButton Content="Custom"
                         GroupName="CalcMode"
                         IsChecked="{Binding Path=CalcMode, 
                                             Converter={StaticResource EnumBooleanConverter}, 
                                             ConverterParameter={x:Static radioButtonBinding:CalcMode.User}}"/>
        </StackPanel>
    </Window>

    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        private CalcMode calcMode = CalcMode.User;
        public CalcMode CalcMode
        {
            get { return calcMode; }
            set
            {
                calcMode = value;
                RaisePropertyChanged("CalcMode");
            }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler == null) return;

            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class EnumBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var paramEnum = parameter as Enum;
            var valueEnum = value as Enum;

            return Equals(paramEnum, valueEnum);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var parameterEnum = parameter as Enum;
            if (parameterEnum == null)
                return DependencyProperty.UnsetValue;

            return parameterEnum;
        }
    }

    public enum CalcMode : byte
    {
        Automatic,

        User,
    }

更新:

我怀疑它一定是 Converter 但我不知道为什么?它只是陷入了一个循环.

I suspect it must be the Converter but I don't know why? It just falls into a loop.

推荐答案

编辑如下将 enum 转换为 bool 呢?

EDIT What about converting the enum to bool as follows?

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (parameter == null || !(bool)value)
        return DependencyProperty.UnsetValue;
    var parameterEnum = parameter as Enum;

    return parameterEnum;
}

这篇关于尝试更改 WPF 中绑定的 RadioButton 时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:尝试更改 WPF 中绑定的 RadioButton 时的奇怪行为