user9035668
user9035668

Reputation:

Pass Data To ViewModel with EventToCommand, Devexpress

I am working with devexpress winform controls and At this moment I have defined the RowDoubleClick event in my view constructor as shown below:

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

The show method in the corresponding viewModel looks like this:

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

When I double click on the row, the messagebox appears and "Row Clicked" is printed, but I want to get the row data (type of student) in this show method too.

How can I do this?

Upvotes: 2

Views: 570

Answers (1)

DmitryG
DmitryG

Reputation: 17850

Take a look at Table (CollectionView) demo. I suggest you split your binding into two parts - binding the Focused row to the ViewModel's property. And than, binding of double click action to the Show command:

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));

ViewModel:

public class MyViewModel{
    public virtual Student SelectedEntity { 
        get;
        set;
    }
    protected void OnSelectedEntityChanged(){
        this.RaiseCanExacuteChanged(x => x.Show(default(Student)));
    }
    public void Show(Student student){
        //...
    }
}

Upvotes: 1

Related Questions