方式一
Xaml文件
<UserControl x:Class="Example.View.MainView" ...
xmlns:ViewModel="clr-namespace:Example.ViewModel"
xmlns:Common="clr-namespace:Example.Common"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
DataContext="{dxmvvm:ViewModelSource Type=ViewModel:MainViewModel}">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox ItemsSource="{Binding Persons}">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="MouseDoubleClick" Command="{Binding EditCommand}">
<dxmvvm:EventToCommand.EventArgsConverter>
<Common:ListBoxEventArgsConverter/>
</dxmvvm:EventToCommand.EventArgsConverter>
</dxmvvm:EventToCommand>
</dxmvvm:Interaction.Behaviors>
<ListBox.ItemTemplate>
...
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
ViewModel
//POCO ViewModel
[POCOViewModel]
public class MainViewModel {
public void Edit(Person person) {
...
}
public bool CanEdit(Person person) {
return person != null;
}
}
//Common ViewModel
public class MainViewModel {
public ICommand<Person> EditCommand { get; private set; }
public MainViewModel() {
EditCommand = new DelegateCommand<Person>(Edit, CanEdit);
}
public void Edit(Person person) {
...
}
public bool CanEdit(Person person) {
return person != null;
}
}
Converter
using DevExpress.Mvvm.UI;
using System.Linq;
public class ListBoxEventArgsConverter : EventArgsConverterBase<MouseEventArgs> {
protected override object Convert(object sender, MouseEventArgs args) {
ListBox parentElement = (ListBox)sender;
DependencyObject clickedElement = (DependencyObject)args.OriginalSource;
ListBoxItem clickedListBoxItem =
LayoutTreeHelper.GetVisualParents(child: clickedElement, stopNode: parentElement)
.OfType<ListBoxItem>()
.FirstOrDefault();
if(clickedListBoxItem != null)
return (Person)clickedListBoxItem.DataContext;
return null;
}
}
在Xaml中设置EvenToCommand,然后通过Converter转换需要的参数,这里可以将多个参数放在list中,最后用Command接收参数
方式二
Xaml
<UserControl x:Class="Example.View.MainView" ...
xmlns:ViewModel="clr-namespace:Example.ViewModel"
xmlns:Common="clr-namespace:Example.Common"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
DataContext="{dxmvvm:ViewModelSource Type=ViewModel:MainViewModel}">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox ItemsSource="{Binding Persons}">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="MouseDoubleClick" Command="{Binding EditCommand}" PassEventArgsToCommand="True">
</dxmvvm:EventToCommand>
</dxmvvm:Interaction.Behaviors>
<ListBox.ItemTemplate>
...
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
ViewModel
//POCO ViewModel
[POCOViewModel]
public class MainViewModel {
public void Edit(Person person) {
...
}
public bool CanEdit(Person person) {
return person != null;
}
}
//Common ViewModel
public class MainViewModel {
public ICommand<MouseEventArgs> EditCommand { get; private set; }
public MainViewModel() {
EditCommand = new DelegateCommand<MouseEventArgs>(Edit, CanEdit);
}
public void Edit(MouseEventArgs arg) {
...
}
public bool CanEdit(MouseEventArgs arg) {
ListBox parentElement = (ListBox)arg.Source;
DependencyObject clickedElement = (DependencyObject)args.OriginalSource;
ListBoxItem clickedListBoxItem =
LayoutTreeHelper.GetVisualParents(child: clickedElement, stopNode: parentElement)
.OfType<ListBoxItem>()
.FirstOrDefault();
Person person;
if(clickedListBoxItem != null)
person = (Person)clickedListBoxItem.DataContext;
return person != null;
}
}
与方法一比较,多设置了PassEventArgsToCommand="True",少了Converter,直接将参数传进Command中,原本的sender可以通过arg.Source取得