当Enum位于ViewModel中时,将Enum值作为CommandParameter传递

Ern*_*e S 9 c# wpf xaml mvvm

我还在学习WPF Binding,并且一直在努力解决这个问题.我在ViewModel中有一个枚举存储,如下所示:

namespace theNamespace
{
    public class frmSetupViewModel
    {
        public enum LocationLabelType {Location, Sample}
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望有一个按钮通过CommandParameter传递其中一个值,但无法弄清楚如何让它工作.到目前为止,这些是我尝试过的组合:

//When value is inside the frmSetupViewModel, these do not work
CommandParameter="{x:Static local:LocationLabelType.Location}" //'Type  was not found.'
CommandParameter="{x:Static local:frmSetupViewModel+LocationLabelType.Location}" //'Type was not found.'
CommandParameter="{x:Static local:frmSetupViewModel.LocationLabelType.Location}" //'Type was not found.'

CommandParameter="{Binding {x:Static local:LocationLabelType.Location}}" //'Value cannot be null'
CommandParameter="{Binding {x:Static local:frmSetupViewModel+LocationLabelType.Location}}" //'Value cannot be null'
CommandParameter="{Binding {x:Static local:frmSetupViewModel.LocationLabelType.Location}}" //'Value cannot be null'
Run Code Online (Sandbox Code Playgroud)

但是,如果我移动枚举OUTSIDE VM并进入名称空间,如下所示:

namespace theNamespace
{
    public enum LocationLabelType {Location, Sample}

    public class frmSetupViewModel
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用:

//Works when enum is moved to Namespace
CommandParameter="{x:Static local:LocationLabelType.Location}"
Run Code Online (Sandbox Code Playgroud)

我假设我的CommandParameter缺少一些东西?

VM通过DataContext加载:

<Window.DataContext>
    <local:frmSetupViewModel />
</Window.DataContext>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Vya*_*kov 5

这工作正常:

CommandParameter="{x:Static uiTest:MainWindow+LocationLabelType.Location}"
Run Code Online (Sandbox Code Playgroud)

你用这段代码运行了这个项目吗?如果您不构建项目, WPF 设计器可能会显示此错误//'Type was not found.',因为它看不到枚举的类型。