Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 - 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.

like image 590
Steve Brouillard Avatar asked May 27 '09 13:05

Steve Brouillard


1 Answers

Just pass command parameter like simple object type. Then you can use direct cast to your enum.

like image 120
Pitka Avatar answered Oct 02 '22 23:10

Pitka