0

What I have: A style resource which contains animation definitions and a view definition, a viewmodel which contains Data, Properties and ICommands.

Currently the application can call VM functions via Button Clicks (using ICommand), View with binding to VM Properties & it is able to run animations from a button.click event trigger.

What I am looking to do; If a style is applied to a listbox(for example) How do I trigger animations from the VM directly? I.E. if some property on the data changes, is it possible to cause an animation to run? I know I could do this with a UserControl, but I'm trying to seperate my program into more clearly defined Views and Viewmodels. I don't want Code in my View, and I don't want animations in my VM.

  • 1
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask] – Inbar Rose Nov 06 '13 at 16:29
  • That isn't mvvm. Just do your UI work in the codebehind. –  Nov 06 '13 at 16:38

2 Answers2

3

You almost answered your own question there when you said that it is able to run animations from a button.click event trigger. The actual answer is yes, you can start an animation using a DataTrigger that is data bound to a view model property, or an EventTrigger using a custom RoutedEvent in the same way:

<Style>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsAnimationRunning}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <SomeAnimation />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

This would start the animation whenever the IsAnimationRunning property is changed from false to true.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

EDIT: On further research, I think what you need is: WPF Command with Click Event Handler

The idea is that you should call the ICommand within the event, allowing further event subscriptions. It still feels messy, since it pretty much ignores the standard command binding syntax, but it amounts to the same thing, since the command binding is still in the view.

Community
  • 1
  • 1
Magus
  • 1,302
  • 9
  • 16
  • This answer should have been a comment, although I didn't down vote you. – Sheridan Nov 06 '13 at 16:15
  • @Sheridan: Fixed, with an answer that actually works. Didn't realize an ICommand stopped the click event. – Magus Nov 06 '13 at 16:21
  • On this site, short answers such as yours still is are generally not preferred. It is generally preferred for some of the linked page(s) to be included in the answer to provide some context. Please see the 'Provide context for links' section of the [Help pages](http://stackoverflow.com/help/how-to-answer) for more information. – Sheridan Nov 06 '13 at 16:25