views:

417

answers:

2

This is a situation that comes up often:

In the View, you have a control bound to a ViewModel property (backed by a INotifyPropertyChanged). For example:

<TextBlock Text="{Binding Path=Subtotal}"/>

When the property changes, you need to bring the user attention to the fact with some creative animation. How I can utilize the fact that the view is already wired to the notification and avoid creating much of the extra code (or at least create it once and re-use). Data triggers are probably the best choice, but I do not know how to make them fire on any value change versus on some specific value.

The following options come to mind:

  • raise an additional event in the ViewModel, subscribe in the View code-behind.
  • create a datatrigger bound to the property mentioned using a convertor that would return true if the value is changing.
  • create a datatrigger bound to a new boolean property on the ViewModel which is used to "signal" the change.
  • create a behavior attached to the control which would subscribe to the control's dependency property change and start the animation.

Which one do you like/use? Did I miss any options?

P.S. It would be nice (but not critical) if the solution would provide a possibility to start the animation first and reflect the value change when it is ended.

A: 

You can create a trigger that will start the animation.

Something like this:

<Style>
    <Style.Triggers>
       <Trigger 
            Property="ViewModelProperty"
            Value="True">
            <Trigger.EnterActions>
                 <BeginStoryboard Storyboard="YourStoryBoard" />
            </Trigger.EnterActions>
       </Trigger>
    </Style.Triggers>
</Style>

As for the issue for the issue of setting the value once the animation has completed, this is a bit of a pain. As far as I'm aware you'd need to use the completed event on the storyboard, this requires code behind, which is something you want to avoid with MVVM.

I've tried using EventTriggers to bind to the completed events, but that also introduces some complications. See here for more details.

Chris Nicol
Again, what if property is not boolean or a set of choices, but an arbitrary string or number?
Sergey Aldoukhov
Then you would use a ValueConverter. Same solution, just with a valueConverter
Chris Nicol
How would a value converter know that the value had changed, unless you created a value converter for each individual binding?
Drew Noakes
If the value changes, it will get passed through the converter again. The Converter doesn't know or need to know anything other than what it takes and what it returns.
Chris Nicol
Can you flesh out the sample code above to explain your answers in these comments, please? I'd like to see exactly what you mean by the use of value converters for animating changes to arbitrarily-typed viewmodel properties. Thanks.
Mal Ross
+3  A: 

Ok, this is what I came to after some experimenting.

I have created an Expression Blend 3 trigger with a dependency property (I named it Subscription). I bind the Subscription to the same value that my TextBlock is bound to and this trigger is attached to a ControlStoryboardAction from Expression Blend 3.

Here's the trigger:

public class DataTriggerPlus : TriggerBase<DependencyObject>
{
    public static readonly DependencyProperty SubscriptionProperty =
        DependencyProperty.Register("Subscription", 
            typeof(string),
            typeof(DataTriggerPlus),
            new FrameworkPropertyMetadata("",
              new PropertyChangedCallback(OnSubscriptionChanged)));

    public string Subscription
    {
        get { return (string)GetValue(SubscriptionProperty); }
        set { SetValue(SubscriptionProperty, value); }
    }

    private static void OnSubscriptionChanged(DependencyObject d,
      DependencyPropertyChangedEventArgs e)
    {
        ((DataTriggerPlus)d).InvokeActions(null);
    }
}

Here's how it is attached to the storyboard:

<TextBlock x:Name="textBlock" Text="{Binding TestProp}" Background="White">
    <i:Interaction.Triggers>
        <local:DataTriggerPlus Subscription="{Binding TestProp}">
            <im:ControlStoryboardAction 
                Storyboard="{StaticResource Storyboard1}"/>
        </local:DataTriggerPlus>
    </i:Interaction.Triggers>
</TextBlock>

I like this approach a lot, great job Blend 3 designers!

Edit: answering Drew comment...

Yes, it ships with Blend. You can just include Microsoft.Expression.Interactions.dll and System.Windows.Interactivity into your project.

And yes, it is verbose (I have asked if somebody figured out a good way to apply behaviours via Styles in this question) - but there is also a benefit of flexibility. For example you can not only start a storyboard, but also switch a state or do some other action from the same trigger.

Sergey Aldoukhov
Where does `<i:Interaction.Triggers>` come from? I'm guessing it ships with Blend. Is it available to us non-Blend users? I want to achieve the same thing in my code, though am hoping that a solution exists that's less verbose as I have many such instances that require this. Maybe it's possible via a Style...
Drew Noakes