Silverlight 3/Prism - Passing an enum value as a Command Parameter

Topics: Prism v2 - Silverlight 2
May 28, 2009 at 1:45 PM

I'm trying to use Prism and the MVVM pattern to develop an application. In my UI, I have a previous and next button defined. For use in a calling web services, I've defined an enum that will tell me the direction I need to traverse. So, in this instance, the buttons map directly to an enum value. The enum definition is very simple and is as follows:

namespace CodeExpert.Book.Helpers
public enum BookDirection { Previous = -1, NotSet = 0, Next = 1, }

I've defined my command and delegate in my ViewModel and assigned the propery correctly. The relevant code is:

    public DelegateCommand PreviousNextCommand { get; set; }
    public IndexEntriesViewModel(GlobalVariables globalVariable, IndexEntryOperations currentOperator)
     //a bunch of initialization code.
    void InitializeCommands()
     PreviousNextCommand =
      new DelegateCommand(OnPreviousNextCommandExecute);
    private void OnPreviousNextCommandExecute(BookDirection parameter)
     //Code to process based on BookDirection

So, based on this config, I want to pass a BookDirection enum value to the CommandParameter.  I can't, however, get the XAML right for this. Here's the XAML I've tried that seems the most correct to me:

<UserControl xmlns=""
     <Button x:Name="ButtonPrevious"
         command:Click.Command="{Binding Path=CurrentIndexViewModel.PreviousNextCommand}">
       <helpers:BookDirection.Previous />

I get no intellisense for the BookDirection for the enum and get an error at design & compile time saying The type 'BookDirection' does not contain adefinition for 'Previous'.  Is there no way to pass that enum, or am I simply missing something?  I have it working now by setting the parameter type to `string` instead of `BookDirection`, but then I have to parse text and the code smells. I've done some Google searches and the closest thing I've come to an answer is here - Unfortunately, Silverlight doesn't support the x:static binding extension, so I can't use the exact technique described in that answer.

Any help would be much appreciated.

May 28, 2009 at 5:19 PM

Hi Corwin,


As you said, there is no x:static in Silverlight, which would be the way to go in WPF. Therefore, I thought of a workaround that might suit your scenario:

1.       Create a class named BookDirectionValues.

2.       In that class, create a property for each of the different enum values. Like this:

public BookDirection Next { get { return BookDirection.Next; } }

3.       Add a resource to your view that represents the class:

            <helpers:BookDirectionValues x:Key="BookDirectionValues" />





4.       Now you can bind you the enum values to your command parameter like this:

command:Click.CommandParameter="{Binding Path=Next, Source={StaticResource BookDirectionValues}}


Take into account that this might not be the best approach, but I hope it is useful for you.


Please let me know if this helps.


Damian Schenkelman