开发者

Using RelayCommand to send complex data to a MVVM model

I have a complex query interface that I need to be passed on via a RelayCommand to my Model. This consists of some comboboxes, checkboxes and text inputs.

I can easily pass on a s开发者_如何学JAVAingle commandparameter (i.e. RelayCommand, etc), but how would I encapsulate the commands, should it be RelayCommand,, etc. This seems a little excessive.

Any hints would be greatly appreciated

Thanks


Acceptable solution is to bind all your comboboxes, checkboxes and text inputs to particular properties of your view model and then use props values in your RelayCommand handler instead of passing data as CommandParameter.

Here is an example:

Your XAML:

<TextBox Text={Binding Arg1} />
<TextBox Text={Binding Arg2} />
<Button Command={Binding Cmd} />

Your ViewModel.cs:

public string Arg1 { get; set; }
public string Arg2 { get; set; }
public ICommand Cmd { get; set; }

... 

Cmd = new RelayCommand(OnCmd);

...

// We will not use command parameter at all
private void OnCmd(object o) 
{
    // Some logic that uses a lot of arguments
    Console.WriteLine(Arg1 + Arg2);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜