wpf - 从 View MVVM WPF在 View 模型中定义的访问枚举类型

标签 wpf xaml mvvm binding

在我的MVVM WPF应用程序中,我声明了一个枚举:

查看模型:

namespace MyViewModel
{
    public class MyViewModelClass  
    {
      public enum MessageTypes
      {
          Info = 0,
          Error = 1
      };
    }
}

现在,从 View 中,我试图访问它,以便将其用作控件中的静态资源,因此:

查看:
xmlns:vm="clr-namespace:MyViewModel;assembly=MyViewModelAssembly"

<Image>
   <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="/Common.Images;component/Images/Info.png"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding MessageTypes}" Value="{x:Static vm:MessageTypes.Error}">
                    <Setter Property="Source" Value="/Common.Images;component/Images/Cancel.png"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
   </Image.Style>
</Image>

这里的问题是无法识别Value="{x:Static vm:MessageTypes.Error}"行。编译错误:
'MessageTypes' type not found.

最佳答案

枚举被声明为嵌套类型(在MyViewModelClass类中),但x:Static标记扩展不支持该枚举类型。

您应该这样声明:

namespace MyViewModel
{
    public enum MessageTypes
    {
        Info = 0,
        Error = 1
    }

    public class MyViewModelClass  
    {
        ...
    }
}

关于wpf - 从 View MVVM WPF在 View 模型中定义的访问枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45323833/

相关文章:

c# - 在 WinRT XAML/C# 中使用 Adorner、AdorrnerLayer、AdornerDecorator、Thumb 移动和调整组件大小

wpf - Xaml:使用子控件触发器更改父控件属性

每个实体的 Android MVVM ViewModel 和存储库?

WPF:带按钮的动态菜单

windows-phone-7 - 我应该将一些属性移动到模型中吗?

wpf - 选择空白行时 DataGrid 上出现意外的红色边框(验证错误)

c# - WPF 简单验证问题 - 设置自定义 ErrorContent

c# - System.Windows.SystemParameters.PrimaryScreenWidth 与转换器的 WPF 绑定(bind)

c# - 比较颜色 WPF

c# - 带有 WPF 内部样式和文本 block 的矩形