tags:

views:

86

answers:

6

I'm having an issue with calling a method on a UserControl. Hear me out:

  1. I have a UserControl someControl in SomeView.xaml

  2. SomeView.xaml's DataContext is SomeViewModel.cs

  3. I want to be able to call someControl.DoStuff() somehow, somewhere.

  4. DoStuff is not UI specific (I could have just called DoStuff from the code-behind of SomeView.Xaml.Cs if it was UI specific, but in this case, it may not be.)

Any ideas?

Thanks!

+2  A: 

You're probably not going to like the answer, but your ViewModel should have no knowledge of your UI. If you have a non-UI method on your UserControl, it's probably in the wrong place.

The only thing I could think of is that you may want to implement some type of messaging (like they have in MVVM Light) that could trigger the execution.

It's either that, or rethink how you've architected your code.

Robaticus
I understand where you are coming from and believe me, I have thought about this. But there comes a time where you really do need to call a method. For example, the method on the user control may be exposed and I may not have the source for the usercontrol so I cannot modify it to accomodate my case.
LB
+1 for the suggestion though, you're right, ViewModel should not have knowledge of the View.
LB
In that case, go to the codebehind, maybe?
Robaticus
Maybe you can reframe question saying, ViewModel trying to access Contract implemented by UI. This will keep ViewModel unaware of UI.
Rakesh Gunijan
A: 

In MVVM design, the idea is generally not to have ANY code in your UserControl (the xaml.cs file) (in an ideal world). All interaction between the UI and the ViewModel should be handled via Commands and Bindings...so why do you need DoStuff in your user control?

You might have something like

<Button Command="{Binding myCommandOnTheDataContextViewModel}" Content="{Binding somePropertyOnTheViewModel}" />
JeffN825
+1  A: 

It sounds like you want DoStuff to happen in response to some data or logic in your VM, in which case the cleanest method would probably be to use an event originating in your VM and handled by DoStuff. If the trigger is more like a state change you could also bind the appropriate VM data to a new Dependency Property on your UserControl and call DoStuff from the DP's change handler.

John Bowen
A: 

Maybe your UserControl should really be a View, which then should have a ViewModel, which would contain the DoStuff() method. SomeViewModel will have instantiated (are at leased casused to be instantiated) SomeControlViewModel, and so be able to call a method on it.

Maxxx
A: 

If you have View-first approach (and your VM is instantiated as Resource in XAML) you may use some normal events to connect your control DoStuff method with some event on VM (on Loaded event).

OldTimer
A: 

If the method DoStuff(); does some UI specific logic then the method is in the right place. When not then it should be in another object (e.g. SomeViewModel).

SomeViewModel is allowed to call a method on SomeView when it is separated via an interface. How this can be accomplished is shown by the WPF Application Framework (WAF).

jbe