2

我正在使用 devexpress winform 控件,此时我已经在视图构造函数中定义了 RowDoubleClick 事件,如下所示:

mvvmContext1.WithEvent<MyViewModel, RowClickEventArgs>(gridView1, "RowClick")
            .EventToCommand(x => x.Show(),
            v => (v.Clicks == 2) && (v.Button == MouseButtons.Left));

对应的 viewModel 中的 show 方法如下所示:

public void Show()
{
    messageBoxService.ShowMessage("Row Clicked");
}

当我双击该行时,会出现消息框并打印“Row Clicked”,但我也想在此显示方法中获取行数据(学生类型)。

我怎样才能做到这一点?

4

1 回答 1

1

看一下Table (CollectionView)演示。我建议您将绑定拆分为两部分 - 将 Focused 行绑定到 ViewModel 的属性。然后,将双击操作绑定到Show命令:

var fluentAPI = mvvmContext.OfType<MyViewModel>();
// Synchronize the ViewModel.SelectedEntity and the GridView.FocusedRowRandle in two-way manner
fluentAPI.WithEvent<ColumnView, FocusedRowObjectChangedEventArgs>(gridView, "FocusedRowObjectChanged")
    .SetBinding(x => x.SelectedEntity,
        args => args.Row as Student,
            (gView, entity) => gView.FocusedRowHandle = gView.FindRow(entity));
// Proceed the Show command when row double-clicked
fluentAPI.WithEvent<RowClickEventArgs>(gridView, "RowClick").EventToCommand(
        x => x.Show(default(Student)),
            x => x.SelectedEntity,
                args => (args.Clicks == 2) && (args.Button == MouseButtons.Left));

视图模型:

public class MyViewModel{
    public virtual Student SelectedEntity { 
        get;
        set;
    }
    protected void OnSelectedEntityChanged(){
        this.RaiseCanExacuteChanged(x => x.Show(default(Student)));
    }
    public void Show(Student student){
        //...
    }
}
于 2018-05-14T09:33:35.087 回答