Silverlight 3 / Prism - passing enum value as a command parameter

I am trying to use Prism and the MVVM pattern to develop an application. In my user interface, I defined the previous and next button. For use in calling web services, I have defined an enumeration that will tell me the direction I need to go. Thus, in this case, the buttons directly display the value of the enumeration. The definition of an enumeration is very simple and looks like this:

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

I defined my team and delegate in my ViewModel and correctly assigned it. Relevant Code:

 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 configuration, I want to pass the value of the BookDirection enumeration to CommandParameter. However, I cannot get XAML for this. Here the XAML I tried seems to me the most correct:

 <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 do not get intellisense for BookDirection for enumeration and get design error and compile time. The type "BookDirection" does not contain a definition for "Previous". Can't I pass this listing, or am I just missing something? Now I work by setting the parameter type to string instead of BookDirection , but then I have to parse the text and the code smells. I did some Google searches, and the closest thing I came to is here Passing an enumeration value as a parameter to a command from XAML . Unfortunately, Silverlight does not support the x: static extension, so I cannot use the exact method described in this answer.

Any help would be greatly appreciated.

+4
source share
4 answers

Bindings are really complex operations, even in WPF. But it seems to be an elegant solution that is available on Caliburn .

The solution, however, is not available in the framework code, but in it LOB Samples . Find the BindableEnum and BindableEnumCollection<T> classes in the Caliburn.Silverlight.ApplicationFramework project.

NTN.

+1
source

First, I'm not sure if you can pass Enum directly from XAML to Silverlight. In any case, there is a small problem in the Prism. See The command that comes before the CommandParameter, what happens when the command receives a lot inside. Prism calls UpdateEnabledState (), which internally calls CanExecute (the object parameter) on your Kommente, passing it a CommandParameter (which has not yet been set)

here is the base code from DelegateCommand.cs in Prism

  bool ICommand.CanExecute(object parameter) { return CanExecute((T)parameter); } 

since the parameter is "null" at this point, the throw throws an exception. Here's how I got around this issue.

Your listing.

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

here is my delegate declaration, pay attention to using an object ... instead of Enum itself. I also expose properties from ViewModel which will expose 2 different directions.

 public DelegateCommand<object> PreviousNextCommand { get; set; } public BookDirection Previous { get { return BookDirection.Previous; }} public BookDirection Next { get { return BookDirection.Next; }} 

now in your OnPreviousNextCommandExecute make sure you get the object and return it to the corresponding enumeration

 private void OnPreviousNextCommandExecute(object parameter) { BookDirection direction = (BookDirection)parameter; //Code to process based on BookDirection } 

and in XAML, they are directly related to the public properties of BookDirection.

 <Button Content="Next" Commands:Click.Command="{Binding PreviousNextCommand}" Commands:Click.CommandParameter="{Binding Next}" /> <Button Content="Previous" Commands:Click.Command="{Binding PreviousNextCommand}" Commands:Click.CommandParameter="{Binding Previous}" /> 

I'm not sure about your binding situation, because in my case I set my DataContext directly in the ViewModel. But that should work for you.

Sorry for my poor English and eloquence, hope this puts you on the right track.

+1
source

I have not tried it myself, but you may have some success in using a ValueConverter from something like a string to your enumeration. That was the impression I got from looking for correspondence in Silverlight xaml.

0
source

Just pass the command parameter as a simple type of object. Then you can use the live stream for your listing.

0
source

All Articles