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

查看:49
本文介绍了尝试在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. 单击MainWindow中的按钮
  2. 将打开ChildWindow,并将RadioButton设置为User
  3. 选择自动",然后再次选择用户"
  4. 关闭ChildWindow并再次将其重新打开!尝试将RadioButton更改为自动".它不会改变!
  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="MainWindow" 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="ChildWindow" 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.

推荐答案

编辑 如下将枚举转换为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时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆