Paul Taylor's blog
Ramblings about technology, music and life...

WPF attached behaviour for double click

Sunday, 6 March 2011 09:05 by paul taylor

I have been working with WPF for a while and as with any technology it takes time to learn.

Being a good citizen, I have been following the MVVM pattern then I came across a problem that left me scratching my head for some time. How to attach a double click behaviour to a list view item. This should be easy (and it is if you know the answer)

I wasted a good hour of my life not being able to get this working, even examples on the web could not help me out. I was getting a bit stressed out and then thought about actually reaching out for a bit of help. I have not used StackOverflow before to ask a question, always been a consumer of the information there. But what the hell, worth a try.

I think the community there is great, I spent the time putting together an example before posting as I know it is easier for someone to help when the question is detailed and not just a “help! I can’t figure this out” few line post.

I could not believe it, 2 minutes after posting I got an answer. Brilliant. Not to repeat myself, here is the info.

The offending line was fixed with this…

<Window.Resources>
<Style x:Key="listViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="local:ClickBehavior.DoubleClick"
Value="{Binding DataContext.Foo, 
RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"/>            
</Style>
</Window.Resources>

The DoubleClick behaviour is relatively simple

public class ClickBehavior
{
public static DependencyProperty DoubleClickProperty =
                  DependencyProperty.RegisterAttached("DoubleClick",
typeof(ICommand),
typeof(ClickBehavior),
new FrameworkPropertyMetadata(null,
                  new PropertyChangedCallback(ClickBehavior.DoubleClickChanged)));

public static void SetDoubleClick(DependencyObject target, ICommand value)
{
target.SetValue(DoubleClickProperty, value);
}

public static ICommand GetDoubleClick(DependencyObject target)
{
return (ICommand)target.GetValue(DoubleClickProperty);
}

public static void ElementMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var element = (UIElement)sender;
var command = (ICommand)element.GetValue(DoubleClickProperty);
command.Execute(null);
}

private static void DoubleClickChanged(DependencyObject target,
Technorati Tags:
DependencyPropertyChangedEventArgs e)
{
var element = target as ListViewItem;
if (element != null)
{
if ((e.NewValue != null) && (e.OldValue == null))
{
element.MouseDoubleClick += ElementMouseDoubleClick;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
element.MouseDoubleClick -= ElementMouseDoubleClick;
}
}
}

Anyway, here is a simple project I put together to help you out if you need it. Enjoy.

John Gossman also has a few great posts with more detailed information on attached behaviours here with a sample.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed
Comments are closed