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

Topics: Prism v2 - Silverlight 2
May 28, 2009 at 2: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.
     InitializeCommands();
    }
   
    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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="CodeExpert.Book.Views.Index"
         d:DesignWidth="1024"
         d:DesignHeight="768"
         xmlns:helpers="clr-namespace:CodeExpert.Book.Helpers"
         xmlns:command="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
         xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
         xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
         xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
         xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input">
   
     <Button x:Name="ButtonPrevious"
         HorizontalAlignment="Left"
         Margin="2,1,0,1"
         Width="25"
         Content="<"
         Grid.Column="1"
         Grid.Row="1"
         Height="20"
         command:Click.Command="{Binding Path=CurrentIndexViewModel.PreviousNextCommand}">
      <command:Click.CommandParameter>
       <helpers:BookDirection.Previous />
      </command:Click.CommandParameter>
     </Button>
   
    </UserControl>


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 - http://stackoverflow.com/questions/359699/passing-an-enum-value-as-command-parameter-from-xaml. 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 6: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:
<UserControl.Resources>

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

            .

            .

            .

</UserControl.Resources>

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

http://blogs.southworks.net/dschenkelman