Extending Command Support

Topics: Prism v2 - Silverlight 2
Apr 1, 2009 at 12:37 AM
Hi - I'm trying to extend command support to a Silverlight DataForm - specifically the ItemEditEnded event.  When I look at the documentation for extending command support, there is a parameter supplied to the register attached property method that is "typeof(Click)".  When I look at the metadata for the Click class, it appears as follows : 

// Summary:

 

// Static Class that holds all Dependency Properties and Static methods to allow

 

 

// the Click event of the ButtonBase class to be attached to a Command.

 

 

//

 

 

// Remarks:

 

 

// This class is required, because Silverlight doesn't have native support for

 

 

// Commands.

 

 

public static class Click

 

{

 

// Summary:

 

 

// Command parameter to supply on command execution.

 

 

public static readonly DependencyProperty CommandParameterProperty;

 

 

//

 

 

// Summary:

 

 

// Command to execute on click event.

 

 

public static readonly DependencyProperty CommandProperty;

 

 

// Summary:

 

 

// Retrieves the System.Windows.Input.ICommand attached to the System.Windows.Controls.Primitives.ButtonBase.

 

 

//

 

 

// Parameters:

 

 

// buttonBase:

 

 

// ButtonBase containing the Command dependency property

 

 

//

 

 

// Returns:

 

 

// The value of the command attached

 

 

public static ICommand GetCommand(ButtonBase buttonBase);

 

 

//

 

 

// Summary:

 

 

// Gets the value in CommandParameter attached property on the provided System.Windows.Controls.Primitives.ButtonBase

 

 

//

 

 

// Parameters:

 

 

// buttonBase:

 

 

// ButtonBase that has the CommandParameter

 

 

//

 

 

// Returns:

 

 

// The value of the property

 

 

public static object GetCommandParameter(ButtonBase buttonBase);

 

 

//

 

 

// Summary:

 

 

// Sets the System.Windows.Input.ICommand to execute on the click event.

 

 

//

 

 

// Parameters:

 

 

// buttonBase:

 

 

// ButtonBase dependency object to attach command

 

 

//

 

 

// command:

 

 

// Command to attach

 

 

public static void SetCommand(ButtonBase buttonBase, ICommand command);

 

 

//

 

 

// Summary:

 

 

// Sets the value for the CommandParameter attached property on the provided

 

 

// System.Windows.Controls.Primitives.ButtonBase.

 

 

//

 

 

// Parameters:

 

 

// buttonBase:

 

 

// ButtonBase to attach CommandParameter

 

 

//

 

 

// parameter:

 

 

// Parameter value to attach

 

 

public static void SetCommandParameter(ButtonBase buttonBase, object parameter);

 

}



What does this class do?  What is the equivalent that I have to implement for my own command extension?  Is this covered in the documentation?  This seems to me to be a missing piece of the puzzle. 

Apr 3, 2009 at 10:31 PM

Hi,

 

To create a command that is executed when the ItemEditEnded is fired you can perform the following steps (remember that this is just a sample). I have smoked tested this sample and it works correctly:

1.       Create a class for your attached behavior with code similar to this. In this example the class’ name is DataFormItemEditEndedCommandBehavior:

public class DataFormItemEditEndedCommandBehavior : CommandBehaviorBase<DataForm>

    {

        public DataFormItemEditEndedCommandBehavior(DataForm dataForm)

            : base(dataForm)

        {

            dataForm.ItemEditEnded += OnItemEditEnded;

        }

 

        private void OnItemEditEnded(object sender, DataFormItemEditEndedEventArgs e)

        {

            ExecuteCommand();

        }

    }

2.       Create a class that will define the needed attached properties to execute the command with code similar to this:

public static class ItemEditEnded

    {

        private static readonly DependencyProperty ItemEditEndedCommandBehaviorProperty = DependencyProperty.RegisterAttached(

            "ItemEditEndedCommandBehavior",

            typeof(DataFormItemEditEndedCommandBehavior),

            typeof(ItemEditEnded),

            null);

 

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(

            "Command",

            typeof(ICommand),

            typeof(ItemEditEnded),

            new PropertyMetadata(OnSetCommandCallback));

 

        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(

           "CommandParameter",

           typeof(object),

           typeof(ItemEditEnded),

           new PropertyMetadata(OnSetCommandParameterCallback));

 

 

        public static ICommand GetCommand(DataForm dataForm)

        {

            return dataForm.GetValue(CommandProperty) as ICommand;

        }

 

        public static void SetCommandParameter(DataForm dataForm, object parameter)

        {

            dataForm.SetValue(CommandParameterProperty, parameter);

        }

 

        public static object GetCommandParameter(DataForm dataForm)

        {

            return dataForm.GetValue(CommandParameterProperty);

        }

 

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)

        {

            DataForm dataForm = dependencyObject as DataForm;

            if (dataForm != null)

            {

                DataFormItemEditEndedCommandBehavior behavior = GetOrCreateBehavior(dataForm);

                behavior.Command = e.NewValue as ICommand;

            }

        }

 

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)

        {

            DataForm dataForm = dependencyObject as DataForm;

            if (dataForm != null)

            {

                DataFormItemEditEndedCommandBehavior behavior = GetOrCreateBehavior(dataForm);

                behavior.CommandParameter = e.NewValue;

            }

        }

 

 

        private static DataFormItemEditEndedCommandBehavior GetOrCreateBehavior(DataForm dataForm)

        {

            DataFormItemEditEndedCommandBehavior behavior = dataForm.GetValue(ItemEditEndedCommandBehaviorProperty) as DataFormItemEditEndedCommandBehavior;

            if (behavior == null)

            {

                behavior = new DataFormItemEditEndedCommandBehavior(dataForm);

                dataForm.SetValue(ItemEditEndedCommandBehaviorProperty, behavior);

            }

            return behavior;

        }

    }

 

With this two classes you are good to go. You will then have to create your command and specify the required binding in your DataForm. The command principles explained in the Composite Application Guidance remain the same.

 

The latter class is the one needed to create the attached behavior when the Xaml parser creates the UserControl. The following blog post uses a similar approach to the code that end up in the CAL (although it is not exactly the same), and explains how the Attached Behavior pattern works:

·         ICommand for Silverlight with Attached Behaviors

 

For more information about customizing commands, you could read:

·         Commands

 

I hope this is useful for you.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman

Apr 4, 2009 at 12:38 AM
Thanks Damian....I went down this road, but I didn't implement the command parameter properly as you have.  Cheers.