DataTriggered animation is triggered only in the first time
Posted
by
Pavel
on Stack Overflow
See other posts from Stack Overflow
or by Pavel
Published on 2014-05-26T21:18:03Z
Indexed on
2014/05/26
21:25 UTC
Read the original article
Hit count: 235
I just wanted to create very simple example of DataTriggers and animation. Two checkboxes and Rectangle. Checking the first cb makes the rectangle fade away. (CodeBehind var is true) Checking the second cb makes the rectangle come back. (var is false)
App is loading - the rectangle is showing (true) Firs cb is checked by default. I'm checking second cb - rect is dissapearing. It's OK. But when I then check the first cb rect isn't showing up. But checking the second cb still makes rect show up and fade away. here's my xaml and code behind:
<StackPanel>
<RadioButton IsChecked="True" Checked="RadioButton_Checked"></RadioButton>
<RadioButton Checked="RadioButton_Checked_1"></RadioButton>
<Rectangle Name="r1" Width="100" Height="300" Fill="Green">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Active}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0" To="1" Duration="0:0:1"
/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding Active}" Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="1" To="0" Duration="0:0:1"
/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</StackPanel>
public bool Active
{
get { return (bool) GetValue(ActiveProperty); }
set { SetValue(ActiveProperty, value); }
}
public static readonly DependencyProperty ActiveProperty =
DependencyProperty.Register("Active", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
Active = true;
}
private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
{
Active = false;
}
© Stack Overflow or respective owner