WPF:列表视图:使用 MVVM 模式在 ListView 项上绑定双击事件 [英] WPF: List-View: Binding double-click event on ListView Items using MVVM pattern
本文介绍了WPF:列表视图:使用 MVVM 模式在 ListView 项上绑定双击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 MVVM 模式我正在绑定 ListView 控件的 Item 源,使用下面的 xaml 代码绑定双击事件,
Using MVVM pattern I am binding the Item source of a ListView control, binded double click event using the below xaml code,
实现使用:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<z:EventToCommand Command="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MouseDoubleClick}"/>
</i:EventTrigger>
当我双击列表视图项时,我无法执行我的功能.
when i double click on the listview items am not able to perform my functionality.
如何在 MVVM 模式中有效地附加双击事件??
How can i attach a double click event in MVVM pattern effecitively??
推荐答案
我在我的项目中使用它.
i use this in my projects.
<DataGrid.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding Path=EditEntityCommand}"
CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItem}"/>
</DataGrid.InputBindings>
对于 ListView,您必须将 Binding 设置为 ListViewItems
ok for ListView you have to set the Binding to the ListViewItems
<ListView x:Name="listView1" Grid.Row="2" ItemsSource="{Binding VmUsers}">
<ListView.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}">
<ContentPresenter.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, ElementName=listView1}"
CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}"/>
</ContentPresenter.InputBindings>
</ContentPresenter>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
或者你使用交互工具
<ListView Name="listView1" ItemsSource="{Binding Cars}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LeftDoubleClick">
<i:InvokeCommandAction Command="{Binding ItemSelectCommand}" CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
这篇关于WPF:列表视图:使用 MVVM 模式在 ListView 项上绑定双击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文