10

我仍在学习 WPF 绑定并且已经为此苦苦挣扎了一段时间。我在 ViewModel 中有一个枚举存储,如下所示:

namespace theNamespace
{
    public class frmSetupViewModel
    {
        public enum LocationLabelType {Location, Sample}
        ...
    }
}

我想让一个按钮通过 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'

但是,如果我将枚举移到 VM 外部并进入名称空间,如下所示:

namespace theNamespace
{
    public enum LocationLabelType {Location, Sample}

    public class frmSetupViewModel
    {
        ...
    }
}

这很好用:

//Works when enum is moved to Namespace
CommandParameter="{x:Static local:LocationLabelType.Location}"

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

VM 通过 DataContext 加载:

<Window.DataContext>
    <local:frmSetupViewModel />
</Window.DataContext>

谢谢。

4

1 回答 1

5

这工作正常:

CommandParameter="{x:Static uiTest:MainWindow+LocationLabelType.Location}"

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

于 2013-04-25T14:36:39.397 回答