How to have an enum value in CommandParam in XAML
-
15-04-2021 - |
Pregunta
I have the following enum that represent a state of UI (I use it to enable and disable UI elements):
enum Mode
{
EDIT, RUN, REVIEW
}
I would like to pass Mode.EDIT to command in CommandParam:
<Button Grid.Column="6" VerticalAlignment="Top Command="{Binding Path=ChangeMode}"
CommandParameter="{StaticResource local:Mode.RUN}" />
But I have no idea how to declare it. As you see in the button declaration, I try to use StaticResource but it fails. I am quite new to SL4 and C# so I suppose that I missed something.
Solución
I have found a solution. I have created in my MyViewModel
(my DataContext) 3 public attributes (of type Mode) and initialize them in the constructor (with values EDIT, RUN, REVIEW). Next, I have bound them in XAML as a normal property of a DataContext
:
CommandParameter="{Binding Path=EDIT}
class MyViewModel
{
public Mode EDIT {set; get;}
public Mode RUN {set; get;}
public Mode REVIEW {set; get;}
MyViewModel()
{
EDIT = Mode.EDIT;
...
}
}
Otros consejos
In WPF we can do something like this (might not work in SL) -
<Button Grid.Column="6" Command="{Binding Path=ChangeMode}"
CommandParameter="{x:Static local:Mode.RUN}" />
check this question for more details - Passing an enum value as command parameter from XAML
in silverlight x:Static doesn't work so we can do sth like this:
<Button Command="{Binding Path=ChangeMode}">
<Button.CommandParameter>
<Mode>RUN</Mode>
</Button.CommandParameter>
</Button>