Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF - Confusing DataTrigger/DoubleAnimation behaviour
    text
    copied!<p>I'm trying to animate the <code>ScaleY</code> property of a <code>LayoutTransform</code> based on a <code>DataTrigger</code> bound to a boolean on my <code>ViewModel</code> class. The animation happens when the value is first seen to be false by the <code>DataTrigger</code> (when the application first starts) and when i first change it to true in a checkbox's checked event but not when i set it to false in the same checkbox's unchecked event.</p> <p>A simplified version of what i'm doing is listed below.</p> <p>The ViewModel class is very simple, containing a single boolean <code>DependencyProperty</code> called Selected.</p> <pre><code> public class VM : DependencyObject { public bool Selected { get { return (bool)GetValue(SelectedProperty); } set { SetValue(SelectedProperty, value); } } // Using a DependencyProperty as the backing store for Selected. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectedProperty = DependencyProperty.Register("Selected", typeof(bool), typeof(VM), new UIPropertyMetadata(false)); } </code></pre> <p>The <code>Window.xaml</code> contains a button and a checkbox. When the checkbox is checked, i set the ViewModel's 'Selected' property to true and false when it is unchecked. Here's the code for both the xaml and it's code-behind.</p> <pre><code>&lt;Window x:Class="DataTriggers.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:y="clr-namespace:DataTriggers" Title="Window1" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;y:VM x:Key="VM"/&gt; &lt;Style TargetType="Button" x:Key="but"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Selected}" Value="False"&gt; &lt;DataTrigger.EnterActions&gt; &lt;BeginStoryboard&gt; &lt;Storyboard&gt; &lt;DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="(LayoutTransform).(ScaleTransform.ScaleY)"/&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/DataTrigger.EnterActions&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding Selected}" Value="True"&gt; &lt;DataTrigger.EnterActions&gt; &lt;BeginStoryboard&gt; &lt;Storyboard&gt; &lt;DoubleAnimation Duration="0:0:1" To="1" Storyboard.TargetProperty="(LayoutTransform).(ScaleTransform.ScaleY)"/&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/DataTrigger.EnterActions&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;Button Style="{StaticResource but}" DataContext="{StaticResource VM}"&gt; &lt;Button.LayoutTransform&gt; &lt;ScaleTransform&gt;&lt;/ScaleTransform&gt; &lt;/Button.LayoutTransform&gt; me &lt;/Button&gt; &lt;CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <pre><code> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void CheckBox_Checked(object sender, RoutedEventArgs e) { VM vm = this.FindResource("VM") as VM; vm.Selected = true; } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { VM vm = this.FindResource("VM") as VM; vm.Selected = false; } } </code></pre> <p>I know that the <code>DataTrigger</code> fires when the property is false because if i change the <code>DoubleAnimation</code> to a simple <code>Setter</code> operating on the <code>Opacity</code> property then i see the correct results. So it would seem to be a problem with how I'm using the <code>DoubleAnimation</code>.</p> <p>Any help would be appriciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload