Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A great solution to this type of problem comes from the usage of Attached Properties. Marlon Grech has taken the usage of Attached Properties to the next level by creating <a href="http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/" rel="nofollow noreferrer">Attached Command Behaviors</a>. Using these it is possible to bind any Command existing in a ViewModel to any Event existing in the view. </p> <p>This is something I use a lot to deal with similar issues with ListBoxes, where I want them to open, or edit or do some action on a double click.</p> <p>In this example I'm using an older version of Attached Command Behaviors, but the effect is the same. I have a style that is used for ListBoxItems which I am explicitly keying to. However, it would be easy enough to create a application or window wide style applying to all ListBoxItems that sets the commands at a much higher level. Then, whenever the event for the ListBoxItem attached to the CommandBehavior.Event property would fire, it instead fires off the attached Command.</p> <pre><code>&lt;!-- acb is the namespace reference to the Attached Command Behaviors --&gt; &lt;Style x:Key="Local_OpenListItemCommandStyle"&gt; &lt;Setter Property="acb:CommandBehavior.Event" Value="MouseDoubleClick" /&gt; &lt;Setter Property="acb:CommandBehavior.Command" Value="{Binding ElementName=uiMyListBorder, Path=DataContext.OpenListItemCommand}" /&gt; &lt;Setter Property="acb:CommandBehavior.CommandParameter" Value="{Binding}" /&gt; &lt;/Style&gt; &lt;DataTemplate x:Key="MyView"&gt; &lt;Border x:Name="uiMyListBorder"&gt; &lt;ListBox ItemsSource="{Binding MyItems}" ItemContainerStyle="{StaticResource local_OpenListItemCommandStyle}" /&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload