none
Animating on dependency property defined in UserControl

    Question

  • Hello All!

    A summary of the problem is that a DP defined in a UserControl is used for triggering/stopping an animation. The problem
    is that changing the DP is not causing the animation to change except when the default value of the DP is changed at
    the DP definition time.

    Details of the problem are as follows:

    I have a UserControl that has a Dependency Property called Blink, as follows:

        /// <summary>
        /// Interaction logic for UserControl1.xaml
        /// </summary>
        public partial class UserControl1 : UserControl
        {
            #region Blink
    
            /// <summary>
            /// Blink Dependency Property
            /// </summary>
            public static readonly DependencyProperty BlinkProperty =
                DependencyProperty.Register("Blink", typeof(bool), typeof(UserControl1),
                    new FrameworkPropertyMetadata((bool)false));
    
            /// <summary>
            /// Gets or sets the Blink property.  This dependency property 
            /// indicates blink on/off.
            /// </summary>
            public bool Blink
            {
                get { return (bool)GetValue(BlinkProperty); }
                set { SetValue(BlinkProperty, value); }
            }
    
            #endregion
    
            public UserControl1()
            {
                InitializeComponent();
            }
        }

    I want to animate a control defined in the corresponding XAML that should "blink" when the Blink DP is true. The
    XAML for this UserControl is as follows:

    <UserControl
      x:Class="BlinkTestApp.UserControl1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:local="clr-namespace:BlinkTestApp"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Width="300"
      Height="300">
      <UserControl.Resources>
        <Storyboard x:Key="blinkSB">
          <ObjectAnimationUsingKeyFrames Duration="0:0:1.0" 
               RepeatBehavior="Forever" 
               Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame 
                KeyTime="0:0:0.5" 
                Value="{x:Static Visibility.Visible}"/>
          </ObjectAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="noBlinkSB">
          <ObjectAnimationUsingKeyFrames Duration="0:0:0.0" 
               RepeatBehavior="Forever" 
               Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame 
                KeyTime="0:0:0.0" 
                Value="{x:Static Visibility.Visible}"/>
          </ObjectAnimationUsingKeyFrames>
        </Storyboard>
        <Style x:Key="blink" TargetType="{x:Type FrameworkElement}">
          <Setter Property="Visibility" Value="Hidden"/>
          <Style.Triggers>
            <Trigger Property="local:UserControl1.Blink" Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource blinkSB}"/>
              </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="local:UserControl1.Blink" Value="False">
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource noBlinkSB}"/>
              </Trigger.EnterActions>
            </Trigger>
          </Style.Triggers>
        </Style>
      </UserControl.Resources>
      <Border Style="{StaticResource blink}">
        <Rectangle Fill="Black"/>
      </Border>
    </UserControl>
    I then use this UserControl as follows:

    <Window
      x:Class="BlinkTestApp.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:local="clr-namespace:BlinkTestApp"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Width="300"
      Height="300"
      Title="Window1">
      <Grid>
        <local:UserControl1 Blink="True"/>
      </Grid>
    </Window>
    The problem is that this does not work. It works if the Blink DP default is set to true. However, setting Blink as shown above does not work.
    Can anyone shed light on why?

    Thanks.
    Thursday, September 10, 2009 4:19 PM

Answers

  • Hi Poohcow,

    I performed a test on your sample code and did reproduce the problem on my side.

    It seems that the trigger defined in the style judge the value of the Blink property only when the UserControl is loaded. The style should be applied to the whole UserControl rather than an element in the UserControl. I modiy the XAML of the UserControl as follows and the trigger works even if the default value of the Blink property is false.

    <UserControl x:Class="WpfApplication1.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Height="300" Width="300">
        <UserControl.Resources>
            <Storyboard x:Key="blinkSB">
                <ObjectAnimationUsingKeyFrames Duration="0:0:1.0" RepeatBehavior="Forever" Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame  KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
            <Style x:Key="blink" TargetType="{x:Type local:UserControl1}">
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <Trigger Property="Blink" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource blinkSB}"/>
                        </Trigger.EnterActions>
                    </Trigger>             
                </Style.Triggers>
            </Style>
        </UserControl.Resources>

        <UserControl.Style>
            <StaticResource ResourceKey="blink"/>
        </UserControl.Style>
        <Border >
            <Rectangle Fill="Orange"/>
        </Border>

    </UserControl>

    Hope this helps.
    If you have any question, please feel freel to let me know.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Linda Liu Thursday, September 17, 2009 3:03 AM
    Wednesday, September 16, 2009 6:05 AM