Execute Method in ViewModel from View

Topics: Questions
Jan 19, 2013 at 7:45 AM
Edited Jan 19, 2013 at 7:46 AM

Hello,

My project is an existing Silverlight app which I'm refactoring to use Catel. I've moved all my "Logic" out of the View to the VM.

My Views are all of System.Windows.Controls.Page which in turn contain various CustomControls.

How do I execute a method in my VM from a Method in my View. What I need is similar to EventToCommand but MethodToCommand or actually Commanding that doesn't require using Binding.

Basically I have a Method in my VM:

void DoSomething(object someParam){

Ask server for stuff which I will pass back to View Async...

}

and I want to call it from the View:

private void MyMethodInView(object someParam){

MyVM.DoSomething(someParam)

}

Thanks in advance.

Coordinator
Jan 19, 2013 at 12:28 PM

Can you give an example where you cannot use a command?

1) Catel provides several behaviors (such as DoubleClickTocommand, etc). Take a look if they can help you out.

2) See my blog article about implementing a non-bindable view correctly using MVVM

Jan 20, 2013 at 2:57 PM
Edited Jan 21, 2013 at 6:03 AM

Hi Geert,

 

Once again thanks for the reply and that over a weekend.

 

My view is a Map with a whole bunch of custom pushpins dynamically created based on data from a database.

 

Firstly the Custom pushpins do not have a Command. On selecting the pushpin I dynamically load xaml (also from the DB) which may vary from pin to pin.

 

My current solution has a number of levels of data

1. The actual View.

2. First level Popup. (Grid as Container)

3. Second level popup. (Grid as Container)

4. n level popup. (Grid as Container)

 

I currently use Dynamic binding using Indexing (i.e. SomeProperty=”{Binding [SomeBinding]}”

 

When the page initially loads I retrieve my data from the server and “Populate” my actual View’s DataContext (using the above Dynamic/Index method, create Pins etc.)

Based on a Pin selected (This was the reason for my initial mail to you) I Execute a Method on the VM which returns data from the server and also populate my First level popup Dynamically. As there are multiple “levels” this procedure is followed for up to n levels.

 

I’ve implemented your suggestion as per your blog and although not tested yet it does compile J 

 

My intention is to have the current VM for the initial view and then separate VM’s for each level. I’m currently setting each level’s (Grid as Container) Datacontext with the above DynamicDataContext. Do you foresee a problem using Catel to do this?

 

Finaly: I need a mechanism to execute methods on the View from the VM. Should I use standard event/delegate or does Catel have a Message Mediator similar to MVVM Light?

 

Regards

 

From: GeertvanHorrik [email removed]
Sent: 19 January 2013 02:29 PM
To: norman@proelec.co.za
Subject: Re: Execute Method in ViewModel from View [Catel:430117]

 

From: GeertvanHorrik

Can you give an example where you cannot use a command?

1) Catel provides several behaviors (such as DoubleClickTocommand, etc). Take a look if they can help you out.

2) See my blog article about implementing a non-bindable view correctly using MVVM

Coordinator
Jan 24, 2013 at 11:30 AM

"My intention is to have the current VM for the initial view and then separate VM’s for each level. I’m currently setting each level’s (Grid as Container) Datacontext with the above DynamicDataContext. Do you foresee a problem using Catel to do this?"

No, Catel is built for this :-)

"Finaly: I need a mechanism to execute methods on the View from the VM. Should I use standard event/delegate or does Catel have a Message Mediator similar to MVVM Light?"

Yes, we have a MessageMediator.

Jan 24, 2013 at 1:02 PM

Hi Geert,

Ok thanks so much for that.

Not sure if I should put this in a new thread but here goes;

As explained I use a DynamicDataContext (as provided by http://www.xavierdecoster.com/post/2010/10/08/silverlight-4-data-binding-to-dynamic-datacontext). Any suggestions how I can implement Catel to provide me with the same functionality or how I can integrate your framework with it?

Regards

From: GeertvanHorrik [email removed]
Sent: 24 January 2013 01:30 PM
To: norman@proelec.co.za
Subject: Re: Execute Method in ViewModel from View [Catel:430117]

From: GeertvanHorrik

"My intention is to have the current VM for the initial view and then separate VM’s for each level. I’m currently setting each level’s (Grid as Container) Datacontext with the above DynamicDataContext. Do you foresee a problem using Catel to do this?"

No, Catel is built for this :-)

"Finaly: I need a mechanism to execute methods on the View from the VM. Should I use standard event/delegate or does Catel have a Message Mediator similar to MVVM Light?"

Yes, we have a MessageMediator.

Coordinator
Jan 24, 2013 at 3:50 PM

No, we don't implement indexers like that. In WPF, we provide custom properties using the Expose attribute, but for all other frameworks this was simple too much work to create.

If you go for the dynamic object, then you will have to do it all yourself because Catel does not and will not support that.