views:

593

answers:

1

Hello,

I'm trying to get a command in my ViewModel executed when the user clicks on a item in a ListView. When I add a ListViewItem in XAML I can just add a MouseBinding to its InputBindings.

<ListView>
<ListView.View>
   <GridView>
      <GridViewColumn Header="Test" />
   </GridView>
   </ListView.View>
   <ListViewItem Content="Item 1" >
      <ListViewItem.InputBindings>
         <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />
      </ListViewItem.InputBindings>
 </ListViewItem>
 </ListView>

But how can this be achived in a databound ListView?

<ListView ItemsSource="{Binding Patients}">
<ListView.View>
    <GridView>
        <GridViewColumn Header="Test" />
    </GridView>
    <!-- How to set the MouseBinding for the generated ListViewItems?? -->
</ListView.View>

I already got a solution by defining ListViewItem style and replacing the ControlTempalte of the ListViewItem. Though, I hope there is an easier solution.

Sincerely, Michael

A: 

Replacing the ControlTemplate on ListViewItem using a style is not a bad solution. In fact, it would probably be my first choice.

Another way of accomplishing the same kind is to use a custom attached property on your ListViewItem style:

<Style TargetType="ListViewItem">
  <Setter Property="local:AddToInputBinding.Binding">
    <Setter.Value>
      <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />    
    </Setter.Value>
  </Setter>
  ...

To do this you need to create the MyBindingHandler.AddBinding attached property:

public class AddToInputBinding
{
  public static InputBinding GetBinding(... // create using propa snippet
  public static void SetBinding(...
  public static readonly DependencyProperty BindingProperty = DependencyProperty.RegisterAttached(
    "Binding", typeof(InputBinding), typeof(AddToInputBinding), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
    {
      ((UIElement)obj).InputBindings.Add((InputBinding)e.NewValue);
    }
  }));
}

This could be expanded to handle multiple bindings, but you get the idea: This class allows you to add an InputBinding inside any style.

This solution may be preferable over what you're doing because the DoubleClick binding is defined directly on the ListBoxItem not on another control inside its template. But I think it mostly just comes down to personal preference.

Ray Burns
I tried your solution but couldn't get it to work. First of all, I think GetBinding() and SetBinding() should be static (doesn't compile if they are not) and secondly, i always get the error "Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty'." when i try to use it from within a style. It works when i don't put it inside a style.
Teodor
I think perhaps you didn't use the propa snippet. If you had, it would have put in the 'static' for you. Also, propa should have fixed your other problem by setting up the `DependencyProperty` correctly. Did you set the third argument to `RegisterAttached`?
Ray Burns
In WPF 3.5 the Command property of the MouseBinding is not a DependencyProperty, so this code snipped won't work. This is fixed in WPF 4.0 which I use atm.See: http://blogs.msdn.com/llobo/archive/2009/10/29/new-wpf-features-key-gesture-binding.aspx
Michael Menne