3

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>

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 ?

4
  • 2
    Please post your solution as an answer - it is not clear in the question and messes up SO queries. Commented Mar 20, 2011 at 1:49
  • Yes, it's perfectly ok to answer your own question. Commented Mar 28, 2011 at 14:32
  • I have a working solution, but it is not yet ideal. Once i work it out completely i will post it here, thanks. Commented Apr 6, 2011 at 15:05
  • Did you ever resolve the refresh issue? I would think that implementing INotifyPropertyChanged on ExecuteQueryParameters would keep the object fresh - so long as you refresh it each time one of the member variables is updated. Commented May 15, 2011 at 14:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i sort of came to similar conculsion.. accept, the parameters object is on ViewA, that way there is no cross view dependency. I just pass viewA's parameters object along with the Command. That way whoever executes the command, doesn't need to know where the properties came from. they're just there.
hey Andrew, so how would you do it if you wanted the framework to be more extensible? i.e. not a "one off"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.