How to run trigger in View from ViewModel appropriately?

1.3k views Asked by At

I would like to take an action in my View after raising an event in ViewModel. I found a solution here but it seems quite complicated. Isn't there any built-in mechanism? I've read also about raising RoutedEvent in ViewModel but this approach requires ViewModel to know type of receiver - is it in accoudance with MVVM pattern?

EDIT :

To be more specific, I need to run undermentioned animation from ViewModel:

<Page.Resources>
    <Storyboard x:Key="MyStoryboard">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MyTextBlock">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Page.Resources>

At first I came up with defining eventTrigger in page.triggers, but it requires routedEvent. Do you have any suggestions?

1

There are 1 answers

1
dev hedgehog On

Raising events in ViewModel and catching them in View is allowed in MVVM.

Take a look at it here:

http://msdn.microsoft.com/en-us/library/gg405484%28v=pandp.40%29.aspx

Best exampe for events being raised in ViewModel and catched by View is the one and only INotifyPropertyChanged event.

Lets go futher more into that mechanism.

The View asks the Data if they have INotifyPropertyChanged interface implemented. If so then the View subscribes to PropertyChanged event.

Now you need to do sort of the same. Make the View ask for that certain event you wish to ask and when found simply subscribe to it. Thats it :)

Btw, if you wish code assistence from us then please first provide code.