microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
697 stars 112 forks source link

[Question] Can I set a value in a ViewModel reacting to an Event in the View? #121

Closed SuperJMN closed 7 years ago

SuperJMN commented 7 years ago

Can I set a value in a ViewModel reacting to an Event in the View?

For instance, when a WebView control starts loading content, the ContentLoading  event is invoked. When it has finished loading the content, the LoadCompleted event is invoked.

I would like to have a property in my ViewModel that is set to true/false when those events are raised. For example, a IsLoading property.

Then, I could show a ProgressRing when appropriate.

skendrot commented 7 years ago

If there is an event on a control, you can use the EventTriggerBehavior. Then you can use the ChangePropertyAction

ghost commented 7 years ago

What will be the TargetObject if I will use ChangePropertyAction though? It is not a control in the view, a property of the ViewModel is the one that must be changed.

nigel-sampson commented 7 years ago

You would set the TargetObject to the view model, typically with a binding expression TargetObject="{Binding}".

ghost commented 7 years ago

Oh, thanks!