c# - 尝试更改 WPF 中绑定(bind)的 RadioButton 时的奇怪行为

标签 c# wpf mvvm radio-button ivalueconverter

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

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

  • 单击主窗口中的按钮
  • ChildWindow 打开并且 RadioButton 设置为 User
  • 选择自动,然后再次选择用户
  • 关闭 ChildWindow 并重新打开它!尝试更改RadioButton到自动。它不会改变!

  •     <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但我不知道为什么?它只是陷入了一个循环。

    最佳答案

    编辑
    如何将枚举转换为 bool 值,如下所示?

    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;
    }
    

    关于c# - 尝试更改 WPF 中绑定(bind)的 RadioButton 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38434625/

    相关文章:

    WPF 与分辨率无关

    c# - 从事件更新 Viewmodel - PropertyChangedEventHandler 为空?

    c# - UserControl 具有传播到内部控件的绑定(bind)

    c# - PFX ConcurrentQueue - 有没有办法从队列中删除特定项目

    wpf - Windows 10 顶部的奇怪 strip (wpf)

    c# - 获取驱动器和目录的图标 : Icon. ExtractAssociatedIcon(filePath) 不起作用?

    c# - 在 Prism 中处理 PreviewMouseDown 和 PreviewMouseUp 事件

    javascript - Controller 收到每个页面请求 2 个请求

    c# - 如何消除 RichTextBox 中段落之间的空格?

    c# - 我应该在每个类还是在基类中声明一次 log4net 记录器?