2013-04-25 78 views
8

我仍然在学习WPF绑定,并且一直在努力。我有视图模型内的枚举店,像这样:当Enum在ViewModel中时,将Enum的值传递给CommandParameter

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' 

但是,如果我移动枚举OUTSIDE 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> 

感谢。

回答

2

这项工作很好:

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

您运行这段代码的项目? 如果您不生成项目,WPF设计人员可能会显示此错误//'Type was not found.',因为它没有看到枚举的类型。

+0

谢谢,这实际上工作!那么是否有办法在Desginer中抑制错误?否则,我将无法看到视图。 – Ernie 2013-04-25 15:00:40

+1

尝试构建项目。 – 2013-04-25 15:01:11

+0

谢谢。没有骰子。试图建立,清理,重建,似乎没有得到它。似乎我必须在命名空间中使用它,如果我想使用设计器:/。这是VS2010,是VS2012与WPF的更好吗? – Ernie 2013-04-25 16:37:54