2015-02-06 97 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