开发者

WPF MVVM Commands: Multiple Command Parameters

开发者 https://www.devze.com 2023-02-18 17:05 出处:网络
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 (i.e. a custom object). Not sure ho

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 (i.e. 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>

updat开发者_StackOverflowe

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.

0

精彩评论

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