Ask Your Question
2

What is the method for animating the BorderThickness of a Button in WPF?

asked 2021-11-09 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-23 10:00:00 +0000

pufferfish gravatar image

One way to animate the BorderThickness of a Button in WPF is to use the DoubleAnimation and Storyboard classes. Here's an example code snippet:

<Button x:Name="MyButton" Content="Click Me">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="BorderThickness" Value="2"/>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BorderThickness"
                                             From="2" To="8" Duration="0:0:1"
                                             AutoReverse="True" RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

In this example, we first set the initial BorderThickness value of the button to 2. Then, we define a Storyboard that contains a DoubleAnimation which targets the BorderThickness property of the button. The animation starts from a value of 2 and goes up to 8, and has a duration of 1 second. We also set AutoReverse to True and RepeatBehavior to Forever, so the animation will oscillate between 2 and 8 pixels. Finally, we use an EventTrigger on the Loaded event to start the animation when the button is loaded in the window.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-09 11:00:00 +0000

Seen: 7 times

Last updated: Jul 23 '22