我试图在 WPF 中弄乱与枚举的绑定,但我不太喜欢它。
您可以通过将属性映射到模型中的相应属性来解决此问题,在 get 方法中,您可以实现对枚举状态的依赖。
例如:
<Button  Height="41" HorizontalAlignment="Center" Style="{StaticResource ButtonStyle}"
                    Margin="407,77,289,0" Name="buttonSearch" VerticalAlignment="Top" Width="137" Click="search_click" 
                    IsEnabled="{Binding IsSearchButtonEnabled}" ...
假设你有这个枚举:
public enum States
{
    StateOne,
    StateTwo,
    StateThree
}
在视图模型中,您可以这样做:
    public bool IsSearchButtonEnabled
    {
        get
        {
            return ((Model.actualState) == States.StateTwo);
        }
    }
要自动更新,您的 ViewModel 必须实现 INotifyPropertyChanged。我使用我的 ViewModels 总是子类化的通用实现来简化事情。它看起来像这样,并且应该在每次调用 InvokePropertyChanged(string propertyName) 时处理更新视图。ViewModel 需要知道它应该更新视图,并且知道何时调用此方法。您可以在模型中使用相同的技术,并在模型中放置一个事件处理程序,通知订阅者虚拟机状态枚举的设置器中的更改。
public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public static event PropertyChangedEventHandler PropertyChangedStatic;
    public void InvokePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    public static void InvokePropertyChangedStatic(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChangedStatic;
        if (handler != null) handler(null, new PropertyChangedEventArgs(propertyName));
    }
}