Ernie S
Ernie S

Reputation: 14250

Pass Enum value as CommandParameter when the Enum is in the ViewModel

I am still learning WPF Binding and have been struggling with this for a while. I have an enum store inside the ViewModel like so:

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

And I would like have a button pass one of the values via the CommandParameter but cannot figure out how to get it to work. So far, these are the combinations I have tried:

//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'

But if I move the enum OUTSIDE the VM and into the name space like this:

namespace theNamespace
{
    public enum LocationLabelType {Location, Sample}

    public class frmSetupViewModel
    {
        ...
    }
}

this works just fine:

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

I assume I am missing something with my CommandParameter?

The VM is loaded via DataContext:

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

Thanks.

Upvotes: 10

Views: 4926

Answers (1)

Vyacheslav Volkov
Vyacheslav Volkov

Reputation: 4742

This work fine:

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

You ran the project with this code? WPF designer can show this error //'Type was not found.' if you don't build the project, because it does not see the type of enum.

Upvotes: 5

Related Questions