将枚举与 propertychanged 处理程序绑定而不引发转换器

Binding enum with propertychanged handler not raising converter

我不是使用 WPF 进行 C# 编程的新手,我从来不需要这样做,但现在我需要它,而且我已经坚持了一段时间。我需要绑定一个附加了它的 OnPropertyChanged 方法的枚举,以便在每次枚举更改时引发一个转换器。我有以下枚举代码:

    private WindowState windowstate;
    public enum WindowState
    {
        INITIAL = 0,
        LANGUAGE = 1,
        SENSOR = 2,
        PARAMETERS = 3,
        LEGAL = 4,
        PRIVACY = 5,
        ABOUT = 6,
        MANUAL = 7
    }
    public WindowState State
    {
        get { return windowstate; }
        set { windowstate = value; OnPropertyChanged("State"); }
    }

在我绑定枚举的 xaml 上,我得到了这个:

Color="{Binding State, Converter={StaticResource ButtonMenuColor}, ConverterParameter=language, ElementName=userControl}"

我想要的是根据枚举值更改按钮的颜色。是否可以这样做,或者 WPF 出于某种原因不支持这种方式?

这是转换器代码:

class ButtonMenuColor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Lynx.Windows.Herramientas.WindowState state = (Lynx.Windows.Herramientas.WindowState)value;
        string param = parameter as string;

        if (state.ToString().ToLower() == param)
            return Application.Current.FindResource("white") as SolidColorBrush;

        return Application.Current.FindResource("buttonmenu_color") as SolidColorBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

似乎您正在绑定到用户控件,但您的 属性 在视图模型中?

所以将绑定更改为

Color="{Binding DataContext.State, Conv...}"

因此您绑定到用户控件视图模型的状态 属性。如果 State 是您的 userControl 的 DependencyProperty,则绑定应该有效。

我想到的是:

change binding mode to two way. add updateSourceTrigger = PropertyChanged , NotifySourceUpdated = True

try a fallback value checking your binding is correct or not.

maybe your control loads before your value set.

and put your enum value as {x:static Namespace:Class.WindowState+LANGUAGE }