MVVM Experts - How would you...

322 views
Skip to first unread message

Karl Shifflett

unread,
Jan 29, 2009, 9:49:23 AM1/29/09
to wpf-di...@googlegroups.com

Got the following two part question today on my blog.

 

I know how I would solve this, but was wondering what the experts would do...

 

I tried the MVVM design pattern, and found a few roadblocks, mostly about events. For example, how do I bind a double click on a listbox item to an event? How do I bind a click on a gridview header (to sort rows) into an event?

 

Cheers,

 

Karl

Corrado Cavalli

unread,
Jan 29, 2009, 9:59:34 AM1/29/09
to wpf-di...@googlegroups.com

That’s the first question I get when I speak about MVVM, my personal approach is to use an attached behavior that links the event to related command.

 

-Corrado

Mike Brown

unread,
Jan 29, 2009, 10:04:17 AM1/29/09
to wpf-di...@googlegroups.com
I agree, sorting a gridview is a perfect example of how attached behaviors are useful ;)

Sacha Barber

unread,
Jan 29, 2009, 11:05:56 AM1/29/09
to wpf-di...@googlegroups.com
I think attached behavior is the way to go to


Subject: [WPF Disciples] MVVM Experts - How would you...
Date: Thu, 29 Jan 2009 06:49:23 -0800
From: ka...@littlerichie.com
To: wpf-di...@googlegroups.com

Beyond Hotmail - see what else you can do with Windows Live Find out more!

Bill Kempf

unread,
Jan 29, 2009, 11:06:56 AM1/29/09
to wpf-di...@googlegroups.com
Yep. Today, it's probably the best answer.

--
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.

War is peace. Freedom is slavery. Bugs are features.

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Jan 29, 2009, 11:08:42 AM1/29/09
to wpf-di...@googlegroups.com

Hey Corrado,

 

Do you have a sample to show me?

 

Cheers,

Laurent

No virus found in this incoming message.
Checked by AVG - http://www.avg.com
Version: 8.0.176 / Virus Database: 270.10.15/1922 - Release Date: 1/29/2009 7:13 AM

Corrado Cavalli

unread,
Jan 29, 2009, 11:20:21 AM1/29/09
to wpf-di...@googlegroups.com

Here it is:

 

  public class SelectionBehavior

  {

    public static DependencyProperty SelectionChangedProperty =

            DependencyProperty.RegisterAttached("SelectionChanged",

            typeof(ICommand),

            typeof(SelectionBehavior),

            new UIPropertyMetadata(SelectionBehavior.SelectedItemChanged));

 

 

    public static void SetSelectionChanged(DependencyObject target, ICommand value)

    {

      target.SetValue(SelectionBehavior.SelectionChangedProperty, value);

    }

 

    private static void SelectedItemChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)

    {

      Selector element = target as Selector;

      if (element == null) throw new InvalidOperationException("This behavior can be attached to Selector item only.");

      if ((e.NewValue != null) && (e.OldValue == null))

      {

        element.SelectionChanged += SelectionChanged;

      }

      else if ((e.NewValue == null) && (e.OldValue != null))

      {

        element.SelectionChanged -= SelectionChanged;

      }

    }

 

    private static void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)

    {

      UIElement element = (UIElement)sender;

      ICommand command = (ICommand)element.GetValue(SelectionBehavior.SelectionChangedProperty);

      command.Execute(null);

    }

  }

 

Use:

 

<ComboBox local:SelectionBehavior.SelectionChanged="{Binding SelectManufacturerCommand}" 

              ItemsSource="{Binding Manufacturers}"

              Height="23" Margin="12,38,15,0" Name="comboBox1" VerticalAlignment="Top" />

 

This invokes SelectManufacturerCommand when listbox selection changed

 

-Corrado

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion, GalaSoft [MVP, MCP]


Sent: giovedì 29 gennaio 2009 17:09
To: wpf-di...@googlegroups.com

Subject: [WPF Disciples] Re: MVVM Experts - How would you...

 

Hey Corrado,

 

Do you have a sample to show me?

 

Cheers,

Laurent

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Corrado Cavalli


Sent: Thursday, January 29, 2009 4:00 PM

No virus found in this incoming message.

Karl Shifflett

unread,
Jan 29, 2009, 11:49:45 AM1/29/09
to wpf-di...@googlegroups.com

You guys are the Bomb.  Nice example Corrado!

Corrado Cavalli

unread,
Jan 29, 2009, 12:09:50 PM1/29/09
to wpf-di...@googlegroups.com

Thank you Sir! ;-)

 

From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Karl Shifflett
Sent: giovedì 29 gennaio 2009 17:50
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Re: MVVM Experts - How would you...

 

You guys are the Bomb.  Nice example Corrado!

Peter O'Hanlon

unread,
Jan 29, 2009, 2:10:42 PM1/29/09
to wpf-di...@googlegroups.com
I just got to this thread and thought I could answer it, and lo and behold Corrado's done a bang up job. Nice one Corrado.
--
Peter O'Hanlon

Marlon Grech

unread,
Jan 29, 2009, 3:16:06 PM1/29/09
to wpf-di...@googlegroups.com
ok... some marketing here...... :P

http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/

AttachedCommandBehaviors :) this is what I use....

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/

Laurent Bugnion, GalaSoft [MVP, MCP]

unread,
Jan 29, 2009, 4:45:57 PM1/29/09
to wpf-di...@googlegroups.com

Perfect, thanks Corrado.

 

Commands were a domain where I must admit I was always a little weak, but since I started using Josh Smith’s RelayCommand, I am totally digging it. With your behavior, it is now possible to use commands for more than “just” ICommandSource controls, this is really cool.

 

Fantastic J

Reply all
Reply to author
Forward
0 new messages