Devexpress 事件绑定ViewModel

方式一

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值