ListView InputBinding MouseBinding 不起作用 [英] ListView InputBinding MouseBinding does not work

查看:39
本文介绍了ListView InputBinding MouseBinding 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 ListView.InputBindings 不起作用?

我已经以同样的方式实现了 Interaction.Triggers 并且效果很好.

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>

如果没有 (System.Windows.Interactivity for Interaction.Triggers) 的话,真的不想使用额外的组件

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

推荐答案

正如@Grx70 在对此答案的评论中提到的,在父 ListViewLeftClick 鼠标手势> 对 ListViewItem 不起作用,因为该项目处理这个手势以获得焦点,所以它不会冒泡该手势.

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.

您可以将 InputBinding 处理转移到 ListViewItem 本身:

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>

您还可以在 this 中阅读有关 InputBinding 如何工作的更多信息问题,有一个答案可以解释这一点.答案建议也创建附加行为.

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.

这篇关于ListView InputBinding MouseBinding 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆