WPF Background change of Border OnMouseDown

谁说我不能喝 提交于 2019-12-08 10:47:09

问题


I'm trying to change the background of a border on left mousebutton down. Button I don't get it. Of Course my gradient has 3 stops.

Eventtrigger:

<EventTrigger  RoutedEvent="UIElement.PreviewMouseLeftButtonDown" SourceName="border">
    <BeginStoryboard x:Name="MouseDown_BeginStoryboard" Storyboard="{StaticResource OnMouseDown}"/>
</EventTrigger>

StoryBoard:

<Storyboard x:Key="OnMouseDown">
    <ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[0].Color" To="Red" />
    <ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[1].Color" To="Red" />
    <ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border" Storyboard.TargetProperty="Background.GradientStops[2].Color" To="Red" />
</Storyboard>

If anybody sees the mistake please tell me ;)


回答1:


I don't see any obvious errors and the sample project I created using the code you posted works just fine. Maybe you can use it to find the source of your problem

<Window.Resources>
    <Storyboard x:Key="OnMouseDown">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Red" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Border Name="border">
        <Border.Background>
            <LinearGradientBrush>
                <GradientStop Color="Red" Offset="0"/>
                <GradientStop Color="Green" Offset="0.5"/>
                <GradientStop Color="Blue" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Border.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown"
                          SourceName="border">
                <BeginStoryboard x:Name="MouseDown_BeginStoryboard"
                                 Storyboard="{StaticResource OnMouseDown}"/>
            </EventTrigger>
        </Border.Triggers>
    </Border>
</Grid>

Update

I tried to add a MouseLeave event as well and it's still working.

Update 2

Added MouseEnter

<Window.Resources>
    <Storyboard x:Key="OnMouseDown">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Red" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Red" />
    </Storyboard>
    <Storyboard x:Key="OnMouseLeave">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Blue" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Blue" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Blue" />
    </Storyboard>
    <Storyboard x:Key="OnMouseEnter">
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[0].Color"
                        To="Green" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[1].Color"
                        To="Green" />
        <ColorAnimation Duration="0:0:0.15"
                        Storyboard.TargetName="border"
                        Storyboard.TargetProperty="Background.GradientStops[2].Color"
                        To="Green" />
    </Storyboard>
</Window.Resources>
<Grid>
    <Border Name="border">
        <Border.Background>
            <LinearGradientBrush>
                <GradientStop Color="Red" Offset="0"/>
                <GradientStop Color="Green" Offset="0.5"/>
                <GradientStop Color="Blue" Offset="1.0"/>
            </LinearGradientBrush>
        </Border.Background>
        <Border.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown" SourceName="border">
                <BeginStoryboard x:Name="MouseDown_BeginStoryboard" Storyboard="{StaticResource OnMouseDown}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave" SourceName="border">
                <BeginStoryboard x:Name="MouseLeave_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseEnter" SourceName="border">
                <BeginStoryboard x:Name="MouseEnter_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter}"/>
            </EventTrigger>
        </Border.Triggers>
    </Border>
</Grid>


来源:https://stackoverflow.com/questions/4809951/wpf-background-change-of-border-onmousedown

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