1

In my MVVM application a have a button in a view. When I press a button I want the run some method in the view, and also some method in the view model. I connected come command to my button, so the command can run some method in the view model. But how can I run some method in view also? I tried to connect a click event also, but it does not work. What is the best way to run functions from view and viewmodel also.

Thanks,

Matvey
  • 163
  • 2
  • 10
  • [Another approach](http://stackoverflow.com/a/6421602/109702): bind to a command in your view's code behind, then call into the viewmodel via an interface implemented by that VM. – slugster Jul 29 '15 at 13:52

3 Answers3

2

You can do this entirely in XAML with interaction triggers:

    <Button Content="Do Something" Click="OnClickHandler"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:cmd ="http://www.galasoft.ch/mvvmlight">

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cmd:EventToCommand Command="{Binding Path=ViewModelCommand1}" />
                <cmd:EventToCommand Command="{Binding Path=ViewModelCommand2}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </Button>

In this case both the Window handler and each of the two viewmodel commands get invoked. If the handler sets e.Handled to true the viewmodel commands don't get called. If you set PassEventArgsToCommand="True" in the cmd:EventToCommand then you can specify a handler that accepts the args; setting Handled to true in the first viewmodel handler won't stop the second one being called but you can still check the value in the second handler manually.

Now, that said I would strongly encourage you to re-evaluate your architecture. Calling code-behind is not MVVM, and in over 7 years of doing this on a daily basis I have yet to see a single case where it was actually needed.

Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • 1
    Calling code behind is fine and it is still MVVM if it is view related code in the code behind of the view. Still a +1 for the rest of your answer though. – slugster Jul 29 '15 at 13:51
  • The problem is that generally speaking any code you place in your code behind be unit-tested, and code that can't be tested really shouldn't be in a commercial quality application. I do actually agree with you in principle, custom controls that aren't application specific are probably a good example where code-behind saves a lot of hassle without breaking MVVM, but in my experience that excludes all but the most simple of interface components. – Mark Feldman Jul 30 '15 at 06:01
1

on the Click event, Execute command.

private void btnClick(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            btn.Command.Execute(btn.CommandParameter);
        }
DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
  • If the button already has a command set, surely this wouldn't work? As OP has already mentioned. Nice idea though. – Mike Eason Jul 28 '15 at 07:59
  • yes you have to command setting from xaml and call the command from view file, and i suggested this because he wanted call to both, in that case this is the possible way he can achieve – DeshDeep Singh Jul 28 '15 at 08:13
0

I would highly question your decision to "run both view and viewmodel method on button click". To me, it seems that what you want is that button runs a viewmodel command which in turn causes some change in view. What if for example it was possible to execute the method not through UI but from somewhere else in the application. Shouldn't the view change also?

For that I recommend creating an interface I[something]View, that the view will implement and which the view model have reference to. Then, the viewmodel can call the method on the interface which will do what you are expecting on the button click.

Euphoric
  • 12,645
  • 1
  • 30
  • 44
  • @Matvey It doesn't have reference to view, it has reference to abstraction that tells view what is expected of it. The dependency direction is still the same. – Euphoric Jul 29 '15 at 11:32