Issue Trying To Get Button Border to Animate on Click

半城伤御伤魂 提交于 2019-12-24 03:48:17

问题


I'm trying to animate the border of a button, but having trouble getting it to work. Here's the XAML I setup:

<Button x:Name="Btn">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard Duration="100" AutoReverse="True"
                            Storyboard.TargetName="Btn"                                     
                            Storyboard.TargetProperty="BorderBrush">
                    <ColorAnimation To="Yellow" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
</Button>

I'm getting this error:

Failed to assign to property 'System.Windows.EventTrigger.RoutedEvent'. [Line: 17 Position: 30]

Any idea why? Total noob at this. I'm using Silverlight 4.


回答1:


Silverlight only supports the Loaded event in an event trigger. For this sort of thing you need to be looking into Visual State Manager.

The Button already supports a Visual State called "Pressed". However in order to change the buttons visual behaviour you need to first copy its default template and place it in a static resource.

You can get a copy of the default style used for button here. Lets assume you give this a x:Key of "MyButtonStyle".

You would add your ColorAnimation to the existing Storyboard for the "Pressed" VisualState.

Then your button xaml should be:

<Button Style="{StaticResource MyButtonStyle}" Content="Click Me" />


来源:https://stackoverflow.com/questions/6123393/issue-trying-to-get-button-border-to-animate-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!