2015-02-06 94 views
0

为什么不ListView.InputBindings工作?ListView InputBinding MouseBinding不起作用

我以相同的方式实施Interaction.Triggers,它工作得很好。

<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> 

真的不想当@ Grx70在此答案的评论提到,在父定义的LeftClick鼠标手势使用额外assmebly是否应该没有(System.Windows.InteractivityInteraction.Triggers

+0

是否可以在ListViewItem上使用InputBindings而不是ListView?只是一个想法。 – 2015-02-06 09:40:51

回答

2

工作ListView将不适用于ListViewItem,因为该项目处理此手势以获得焦点,因此它不会将该手势向上泡泡。

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

<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> 

你也可以阅读更多关于如何InputBinding的工作在this qestion,有一个答案解释。答案建议也创建一个附加的行为。

+0

我不确定命令绑定是否只适用于集中控制。显然,处理从集中控制开始,但根据[这个问题和答案](http://stackoverflow.com/questions/12941707/keybinding-in-usercontrol-doesnt-work-when-textbox-has-the- focus)手势也泡了起来。另一方面,他们似乎停止处理(这将与事件冒泡一致),并且我认为'MouseClick'手势由所有可聚焦控件处理以获得焦点 - 从而防止其冒泡。 – Grx70 2015-02-06 10:11:38

+0

@ Grx70,感谢您的改进,我已经更新了我的答案。 – dymanoid 2015-02-06 10:25:00