WPF / SL: ICommands Libraries?

Topics: Prism v2 - Silverlight 2, Prism v2 - WPF 3.5
Jul 8, 2009 at 9:20 PM

Hi there,

First i would like to warn you that im all very new to the Prism framwork and in general in WPF any claims of me should therefore be validated.
And please, if im wrong tell me!

As far as I understand, Prism are only coming out of the box with a few Delegate Commands you can use directly in your ViewModels for a very few standard WPF controls, e.g. the Button control.
Please have a look on this EXTREMLY valuable video http://development-guides.silverbaylabs.org/Video/Prism-Commands

My question is, wouldnt it be possible to do a sort of contrib library containing DelegateCommands that maps to the normal events we find in the WPF controls so we
can use them directly in our ViewModels / Controllers (like shown in the video link above)?

                    ItemsSource="{Binding RootDirectories}"
                    commands:TreeViewItemSelectedChanged.Command="{Binding TreeViewItemSelectedCommand}"

Which would be implemented as (this class must be able to be shared among us or what?) :

 public static class TreeViewItemSelectedChanged
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(TreeViewSelectedItemChanged),
                                                new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty MouseOverCommandBehaviorProperty =
                                                typeof(TreeViewSelectedItemChangedBehaviour), typeof(TreeViewSelectedItemChanged), null);

        public static ICommand GetCommand(DependencyObject obj)
            return (ICommand) obj.GetValue(CommandProperty);

        public static void SetCommand(DependencyObject obj, ICommand value)
            obj.SetValue(CommandProperty, value);

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
            var element = dependencyObject as Control;
            if (element != null)
                TreeViewSelectedItemChangedBehaviour behavior = GetOrCreateBehavior(element);
                behavior.Command = e.NewValue as ICommand;

        private static TreeViewSelectedItemChangedBehaviour GetOrCreateBehavior(Control element)
            var behavior = element.GetValue(MouseOverCommandBehaviorProperty) as TreeViewSelectedItemChangedBehaviour;

            if (behavior == null)
                behavior = new TreeViewSelectedItemChangedBehaviour(element);
                element.SetValue(MouseOverCommandBehaviorProperty, behavior);

            return behavior;

        public static TreeViewSelectedItemChangedBehaviour GetMouseOverCommandBehavior(DependencyObject obj)
            return (TreeViewSelectedItemChangedBehaviour)obj.GetValue(MouseOverCommandBehaviorProperty);

        public static void SetMouseOverCommandBehavior(DependencyObject obj, TreeViewSelectedItemChangedBehaviour value)
            obj.SetValue(MouseOverCommandBehaviorProperty, value);
Have i missed something here or would this be possible? If this would be possible i think at least it would make the Prims framework even more valuable, to increase the time to get things solved. 
Again, correct me if im wrong, im on thin ice here!!!



Jul 9, 2009 at 10:40 AM

Hi Nic,

You might want to take a look at Marlon Grech's attached command behaviors.