Click here to Skip to main content
11,647,170 members (70,053 online)
Click here to Skip to main content

Selector DoubleClick Behaviour calling ViewModel ICommand

, 8 Sep 2009 CPOL 27.8K 14
Rate this:
Please Sign up or sign in to vote.
The other day I was at work and needed to use a WPF ListView (Selector) to call a ICommand in a ViewModel. Here is what I did.

The other day I was at work and needed to use a WPF ListView (Selector) to call an ICommand in a ViewModel. Nown we want to be good and use nice design approaches, so I thought about using the attached command capabilities of my Cinch MVVM framework. But then I thought, ah, I only want to call the ICommand when the user actually double clicks an Item in the ListView (Selector) and not when the double click occurs anywhere else, like say a header, which the ListView certainly has.

So without further ado, I set to work and came up with the following attached Behaviour DP:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Windows;
   4:  using System.Windows.Controls.Primitives;
   5:  using System.Windows.Input;
   6:  using System.Windows.Controls;
   7:   
   8:  namespace ListViewDoubleCLick
   9:  {
  10:      /// <span class="code-SummaryComment"><summary>
</span>

Where we would use this in XAML:

   1:  <Window x:Class="ListViewDoubleCLick.Window1"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      xmlns:local="clr-namespace:ListViewDoubleCLick"
   5:      xmlns:interactivity="clr-       namespace:Microsoft.Expression.Interactivity;
   6:         assembly=Microsoft.Expression.Interactivity" 
   7:      Title="Window1" Height="600" Width="800" 
   8:      WindowStartupLocation="CenterScreen">
   9:      <Grid>
  10:   
  11:          <TabControl>
  12:              <TabItem Header="Attached DP Approach">
  13:                  <ListView ItemsSource="{Binding People}"
  14:                    IsSynchronizedWithCurrentItem="True"
  15:                    local:NaiveSelectorDoubleClickCommandBehavior.HandleDoubleClick="true"
  16:                    local:NaiveSelectorDoubleClickCommandBehavior.TheCommandToRun=
  17:                            "{Binding Path=DoItCommand}" >
  18:   
  19:                      <ListView.View>
  20:                          <GridView>
  21:                              <GridViewColumn Header="FirstName"
  22:                                      DisplayMemberBinding="{Binding FirstName}"
  23:                                      Width="80" />
  24:                              <GridViewColumn Header="LastName" 
  25:                                      DisplayMemberBinding="{Binding LastName}"  
  26:                                      Width="80"/>
  27:                          </GridView>
  28:                      </ListView.View>
  29:                  </ListView>
  30:              </TabItem>
  31:          </TabControl>
  32:      </Grid>
  33:   
  34:  </Window>

Or, we could get really fancy and use the Blend 3 Microsoft.Expression.Interactivity.Dll, which would look something like this:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Windows;
   4:  using System.Windows.Controls.Primitives;
   5:  using System.Windows.Input;
   6:  using System.Windows.Controls;
   7:  using Microsoft.Expression.Interactivity;
   8:  using System.ComponentModel;
   9:   
  10:  namespace ListViewDoubleCLick
  11:  {
  12:      /// <span class="code-SummaryComment"><summary>
</span>

Which we could use from XAML as follows:

   1:  <Window x:Class="ListViewDoubleCLick.Window1"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      xmlns:local="clr-namespace:ListViewDoubleCLick"
   5:      xmlns:interactivity="clr-namespace:Microsoft.Expression.Interactivity;
                                assembly=Microsoft.Expression.Interactivity" 
   6:      Title="Window1" Height="600" Width="800" 
   7:      WindowStartupLocation="CenterScreen">
   8:      <Grid>
   9:   
  10:          <TabControl>
  11:               <TabItem Header="Using Blend3 Interactivity Dll" >
  12:                  <ListView ItemsSource="{Binding People}" 
  13:                    IsSynchronizedWithCurrentItem="True">
  14:   
  15:                      <interactivity:Interaction.Triggers>
  16:                          <interactivity:EventTrigger EventName="MouseDoubleClick">
  17:                              <local:InteractionsSelectorDoubleClickCommandAction 
  18:                                  Command="{Binding DoItCommand}" 
  19:                                  SyncOwnerIsEnabled="True" />
  20:                          </interactivity:EventTrigger>
  21:                      </interactivity:Interaction.Triggers>
  22:   
  23:                      <ListView.View>
  24:                          <GridView>
  25:                              <GridViewColumn Header="FirstName"
  26:                                      DisplayMemberBinding="{Binding FirstName}"
  27:                                      Width="80" />
  28:                              <GridViewColumn Header="LastName" 
  29:                                      DisplayMemberBinding="{Binding LastName}"  
  30:                                      Width="80"/>
  31:                          </GridView>
  32:                      </ListView.View>
  33:                  </ListView>
  34:              </TabItem>
  35:          </TabControl>
  36:          
  37:   
  38:   
  39:      </Grid>
  40:   
  41:  </Window>

As usual, here is a small demo project:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...

Comments and Discussions

 
GeneralArticle Pin
zdebyman25-Oct-12 2:09
memberzdebyman25-Oct-12 2:09 
QuestionWhat do you mean by 'DoItCommand'? Pin
Bob Brady12-Sep-12 10:24
memberBob Brady12-Sep-12 10:24 
AnswerRe: What do you mean by 'DoItCommand'? Pin
Sacha Barber12-Sep-12 19:27
mvpSacha Barber12-Sep-12 19:27 
AnswerRe: What do you mean by 'DoItCommand'? Pin
Sacha Barber12-Sep-12 19:30
mvpSacha Barber12-Sep-12 19:30 
GeneralRe: What do you mean by 'DoItCommand'? Pin
Bob Brady13-Sep-12 4:12
memberBob Brady13-Sep-12 4:12 
QuestionMicrosoft.Expression.Interactivity Pin
brettles2-Sep-12 21:46
memberbrettles2-Sep-12 21:46 
BugPotential Memory Leak Pin
Clifford Nelson7-Jun-12 8:18
memberClifford Nelson7-Jun-12 8:18 
From what I understand, you really need to add a removal of the event handler in the OnHandleDoubleClickChanged when you get a false.
GeneralRe: Potential Memory Leak Pin
Sacha Barber7-Jun-12 9:33
mvpSacha Barber7-Jun-12 9:33 
QuestionImplemented in Cinch? Pin
aaj2318-Apr-12 3:01
memberaaj2318-Apr-12 3:01 
AnswerRe: Implemented in Cinch? Pin
Sacha Barber18-Apr-12 3:09
mvpSacha Barber18-Apr-12 3:09 
GeneralDon't see the benefit anymore with so much code for just a doubleclick... Pin
gvdpeer24-Mar-10 10:32
membergvdpeer24-Mar-10 10:32 
GeneralRe: Don't see the benefit anymore with so much code for just a doubleclick... Pin
Sacha Barber24-Mar-10 12:00
mvpSacha Barber24-Mar-10 12:00 
GeneralReally complex for "just" an DoubleClick Pin
ChrDressler13-Dec-09 8:12
memberChrDressler13-Dec-09 8:12 
GeneralRe: Really complex for "just" an DoubleClick Pin
Sacha Barber13-Dec-09 10:16
mvpSacha Barber13-Dec-09 10:16 
GeneralPassing the double clicked item Pin
Member 424119529-Oct-09 16:08
memberMember 424119529-Oct-09 16:08 
AnswerRe: Passing the double clicked item Pin
Member 424119529-Oct-09 17:17
memberMember 424119529-Oct-09 17:17 
GeneralRe: Passing the double clicked item Pin
Sacha Barber29-Oct-09 20:42
mvpSacha Barber29-Oct-09 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.2 | Last Updated 8 Sep 2009
Article Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid