Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mvvm

wpf

xaml

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.

like image 260
Ernie S Avatar asked Apr 25 '13 14:04

Ernie S


1 Answers

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.

like image 192
Vyacheslav Volkov Avatar answered Sep 25 '22 19:09

Vyacheslav Volkov