从视图MVVM WPF在视图模型中定义的访问枚举类型 [英] Access enumeration type defined in view model from the view MVVM WPF

查看:106
本文介绍了从视图MVVM WPF在视图模型中定义的访问枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my MVVM WPF app, I have declared an enumeration:

查看模型:

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

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

Now from the view I am trying to access it in order to use it as static resource in a control so:

查看:

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}".编译错误:

The problem here is that line Value="{x:Static vm:MessageTypes.Error}" is not being recognized. Compilation error:

'MessageTypes' type not found.

推荐答案

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

The enum is declared as a nested type (in class MyViewModelClass), which is not supported by the x:Static markup extension.

您应该这样声明:

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

    public class MyViewModelClass  
    {
        ...
    }
}

这篇关于从视图MVVM WPF在视图模型中定义的访问枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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