Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:30:44+00:00 2026-06-07T05:30:44+00:00

After getting the animation on an image to run which I got from here:

  • 0

After getting the animation on an image to run
which I got from here: Animate image in a button
I want to be able to switch the animation on and off, depending
on a button click from outside, i.e. from the ViewModel

So I added a new DependencyProperty to the Bahavior (with all those things that are needed here)

 public static readonly DependencyProperty IsShakingProperty =
        DependencyProperty.Register(IsShakingName,
                                    typeof(bool),
                                    typeof(ShakeBehavior),
                                    new PropertyMetadata(DefaultIsShaking));

I have added a new public property to my ViewModel

public bool IsShaking { get; set; }

But what can I do to switch the animation on and off, depending on the
ViewModel property set to true or false? (I want to control the animation
on a button click)

Here is some of the code of which i think it is relevant

private Timeline CreateAnimationTimeline()
{
    DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();

    animation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(0).(1)", UIElement.RenderTransformProperty, RotateTransform.AngleProperty));

    int keyFrameCount = 8;
    double timeOffsetInSeconds = 0.25;
    double totalAnimationLength = keyFrameCount * timeOffsetInSeconds;
    double repeatInterval = RepeatInterval;
    bool isShaking = IsShaking;

    // Can't be less than zero and pointless to be less than total length
    if (repeatInterval < totalAnimationLength)
        repeatInterval = totalAnimationLength;

    animation.Duration = new Duration(TimeSpan.FromSeconds(repeatInterval));

    int targetValue = 12;
    for (int i = 0; i < keyFrameCount; i++)
        animation.KeyFrames.Add(new LinearDoubleKeyFrame(i % 2 == 0 ? targetValue : -targetValue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i * timeOffsetInSeconds))));

    animation.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalAnimationLength))));
    return animation;
}

Here is the part of my XAML:

<ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Focusable="False" Command="{Binding ClickToolCommand}" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
                        <Image Source="myImage.png" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
                            <i:Interaction.Behaviors>
                                <local:ShakeBehavior RepeatInterval="1" SpeedRatio="3.0" IsShaking="{Binding Path=IsShaking}"/>
                            </i:Interaction.Behaviors>

                        </Image>
                    </Button>
                </DataTemplate>
            </ListBox.ItemTemplate>

Perhaps a DataTrigger can help, as pointed out in other SOs, but I do not have a storyboard inside my XAML, as I have a custom Behavior

Any input highly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:30:45+00:00Added an answer on June 7, 2026 at 5:30 am

    Firstly, let me repeat what I said in the original post that started this whole thing. Doing a “shaky” image button becomes a whole lot simpler if you just use a custom control. Also, using a shaky image button as “a way to engage a users attention” is a horrible idea that reminds me of 1990’s website design. In addition, there is a small flaw in the implementation you copied, there is no exit action on the trigger that was created. Regardless, here is how to do what you require:

    Create an attached property as follows:

            public static bool GetStopAnimating(DependencyObject obj)
            {
                return (bool)obj.GetValue(StopAnimatingProperty);
            }
    
            public static void SetStopAnimating(DependencyObject obj, bool value)
            {
                obj.SetValue(StopAnimatingProperty, value);
            }
    
            // Using a DependencyProperty as the backing store for StopAnimating.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty StopAnimatingProperty =
                DependencyProperty.RegisterAttached("StopAnimating", typeof(bool), typeof(ShakeBehavior), new UIPropertyMetadata(true));
    

    Then replace the existing “OnAttach” method with the folllowing:

            private BeginStoryboard _beginStoryBoard;
            private RemoveStoryboard _removeStoryboard;
    
            protected override void OnAttached()
            {
                _orignalStyle = AssociatedObject.Style;
    
                _beginStoryBoard = new BeginStoryboard { Storyboard = CreateStoryboard() };
                _beginStoryBoard.Name = "terribleUi";
                _removeStoryboard = new RemoveStoryboard();
                _removeStoryboard.BeginStoryboardName = _beginStoryBoard.Name; 
    
                AssociatedObject.Style = CreateShakeStyle();
    
                AssociatedObject.Style.RegisterName("terribleUi", _beginStoryBoard);
            }
    

    Then instead of having the shaking trigger based off the visibility of the button, change it to work off your attached property:

            private Trigger CreateTrigger()
            {
                Trigger trigger = new Trigger
                {
                    Property = StopAnimatingProperty,
                    Value = false,
                };
    
                trigger.EnterActions.Add(_beginStoryBoard);
    
                trigger.ExitActions.Add(_removeStoryboard);
    
                return trigger;
            }
    

    Then you use it as follows:

    <Button Height="50" Width="150" >
            <StackPanel>
                <Image Source="\Untitled.png" local:ShakeBehavior.StopAnimating="{Binding YourPropertyToStopTheShaking}">
                    <i:Interaction.Behaviors>
                            <local:ShakeBehavior RepeatInterval="5.0" SpeedRatio="3.0"  />
                    </i:Interaction.Behaviors>
                </Image>
    
            </StackPanel>
        </Button>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After getting a helpful answer here , I have run into yet another problem:
I want to search a user from LDAP and after getting the user I
After getting a profile hash from the LinkedIn Ruby Gem, but I'm looking for
After getting a struct from C# to C++ using C++/CLI: public value struct SampleObject
After getting a input from the first page, I passed it in to the
I've got a jQuery animation that I want to start when the page loads.
Edited after getting answers Some excellent answers here. I like Josh's because it is
how do i (after getting the right stock prices from a source) show it
after getting my answer here: Database issue, how to store changing data structure i
In the below code how to remove the hyperlink after getting the innerHTML :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.