0
votes

I have a MVVM application. Well, it's not exactly 100% pure MVVM for various reasons, but I'm trying to follow its most important principles.

I want to call Foobar method (which executes purely view-related logic) of MyUserControl class contained in MyUserControl.xaml.cs on Button click. Both Buttonand MyUserControl instance exist in the same PageView (whose data context is some irrelevant view-model).

<Button Click="Foobar"/> doesn't work, because the stub appears in PageView.xaml.cs instead of MyUserControl.xaml.cs.

Is it possible to link Button's Click event with Foobar invocation in XAML?

2

2 Answers

2
votes

If you really need to do it via XAML you can try to add a public property ICommand to your MyUserControl class and execute Foobar method in this command. Then bind it in xaml of PageView like this:

<Button Command="{Binding ElementName=myUserControl, Path=FoobarCommand}" />
0
votes

Yhea it is possible. Use the ICommand as it is supposed to in MVVM. You then can simply call you method like that:

<Button Command={Binding CFoobar} .../>

With CFoobar being the Command for your Method in your Viewmodel (DataContext). I hope we are talking about the same problem.