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
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
--
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.
War is peace. Freedom is slavery. Bugs are features.
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
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.
You guys are the Bomb. Nice example Corrado!
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!
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