2

Why does not ListView.InputBindings work?

I've implemented Interaction.Triggers the same way and it works just fine.

<ListView Name="listView1" ItemsSource="{Binding Cars}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseClick">
            <i:InvokeCommandAction Command="{Binding ItemSelectCommand}" CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <ListView.InputBindings>
        <MouseBinding Gesture="LeftClick" Command="{Binding ItemSelectCommand}" CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}"/>
    </ListView.InputBindings>
</ListView>

Really don't want to use that extra assmebly if it should work without (System.Windows.Interactivity for Interaction.Triggers)

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Jason94
  • 13,320
  • 37
  • 106
  • 184

1 Answers1

4

As @Grx70 mentions in the comment to this answer, the LeftClick mouse gesture defined in the parent ListView won't work for a ListViewItem because that item handles this gesture to gain focus, so it doesn't bubble that gesture up.

You could shift your InputBinding processing to the ListViewItem itself:

<ListView Name="listView1" ItemsSource="{Binding A}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding}">
                <ContentPresenter.InputBindings>
                    <MouseBinding Gesture="LeftClick" Command="{Binding DataContext.ItemSelectCommand, ElementName=listView1}" CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}"/>
                </ContentPresenter.InputBindings>
            </ContentPresenter>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

You could also read more about how InputBindings work in this qestion, there is an answer explaining that. The answer suggests to create an attached behavior also.

Community
  • 1
  • 1
dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • I'm not sure if it's true that command bindings work only for focused controls. Apparently handling starts with the focused control, but according to [this question and answers](http://stackoverflow.com/questions/12941707/keybinding-in-usercontrol-doesnt-work-when-textbox-has-the-focus) gestures also bubble up. On the other hand they seem to stop as soon as they're handled (which would be consistent with event bubbling), and I presume `MouseClick` gesture is handled by all focusable controls to gain focus - thus preventing it from bubbling up. – Grx70 Feb 06 '15 at 10:11
  • I'm not sure how this will work with defined columns inside a GridView (using the ListView.View attribute) – Matthew S Sep 06 '18 at 17:29