开发者

Change Background on DragEnter

开发者 https://www.devze.com 2023-04-08 12:27 出处:网络
I want to change the background of a framework element when the DragEnter event is fired and revert its background when the DragLeave event is fired. Additionally, I want this applied in a style.

I want to change the background of a framework element when the DragEnter event is fired and revert its background when the DragLeave event is fired. Additionally, I want this applied in a style.

Heres what I have currently:

<EventTrigger RoutedEvent="Button.DragEnter">
            <BeginStoryboard x:Name="DragHoverStoryboard">
                <Storyboard>
                    <DoubleAnimation Storyboard.Target="??????????"
                                     Storyboard.TargetProperty="Background"
                                     Duration="0:0:0"
                                     To="{DynamicResource HoverBrush}" />
                </Storyboard>
            </BeginStor开发者_高级运维yboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.DragLeave">
            <StopStoryboard BeginStoryboardName="DragHoverStoryboard" />
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.Drop">
            <StopStoryboard BeginStoryboardName="DragHoverStoryboard" />
        </EventTrigger>

The problem here is that I can't apply target by a name because this style can be applied to any FrameworkElement. How do I apply the target to the element that the Style is attached to?


Storyboard.Target is not the problem, just leave it out. However, you need to change the rest of the animation. To animate a color, use a ColorAnimation instead of a DoubleAnimation. Also, the property "Background" does not contain a color but a brush, so animate the property "Background.Color" instead. Here is a working example:

<Style TargetType="Button">
    <Setter Property="Background" Value="Red"/>
    <Style.Triggers>
        <EventTrigger RoutedEvent="Button.DragEnter">
            <BeginStoryboard x:Name="DragHoverStoryboard">
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Background.Color" 
                                    Duration="0:0:0" To="Green" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.DragLeave">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Background.Color" 
                                    Duration="0:0:0" To="Red" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>
0

精彩评论

暂无评论...
验证码 换一张
取 消