简体   繁体   中英

WPF MVVM Commands: Multiple Command Parameters

Perhaps my problem is more architectural than functional, but I am trying to bind a TextBox to a command, and in that command, i'd like to pass multiple parameters (ie a custom object). Not sure how to do this in declarative fashion (xaml).

ViewA (sits on top of ViewModelA) it has a TextBox, which is bound to CommandX

ViewB (sits on top of ViewModelB) (this is in fact another user control within the same Window as ViewA) When commandX fires, ViewModelB needs to execute some method. ViewModelB needs to be updated with multiple properties before it can execute that method.

My question is, how can i structure my command/architecture, so that ViewModelB has sufficient information to execute its method.

side note: ViewModelA has all the necessary info for ViewModelB to execute its method. but, i don't want to get it from there, because later i'd want CommandX to be executed from different views

update

it appears that i could set CommandParameter property separately, which could likely be bound to a complex type in ViewModelA. That should be sufficient to suff all necessary properties into it.

awesome

this worked! here is what my parmeter property looks like on ViewA

    public ExecuteQueryCommandParameters ExecuteQueryParameters {
        get {
            var p = new ExecuteQueryCommandParameters();
            p.AllColumns = ColumnsMaster;
            p.DatabaseName = SelectedDatabase;
            p.ServerName = SelectedServer;
            p.TopRows = 20;
            p.ViewModelName = "MainDataView";

            return p;
        }
    }

and the button implementation

<Button Command="{Binding ExecuteQuery}" CommandParameter="{Binding ExecuteQueryParameters}">Top 20</Button>

update

there is a small road block with this solution. the commandparameter is bound to the property. but it retrieves it at load time, not at the time of command being executed. I need it to happen at command execution time, so that all the properties have latest values. any ideas on how to achieve that ?

Here's how I would do it:

For this explanation, I'll assume all of the necessary information is in one class, called "MyCustomCommandParameters".

I'll also assume that your XAML sets the main parent control's data binding to ViewModelA.

Have ViewModelA expose a property of type MyCustomCommandParameters.

Then, on ViewB, create a DependencyProperty of a type MyCustomCommandParameters. The handlers for this property would pass this information to ViewModelB.

In the XAML, bind the ViewB's "MyCustomCommandParameters" value to ViewModelA's "MyCustomCommandParameters" property.

It's a bit of one-off plumbing work, but then you would meet your goal of having the information passed from ViewModelA to ViewModelB by XAML binding, and the two models would not know about one another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM