WPF Изменение фона Border OnMouseDown - PullRequest
0 голосов
/ 26 января 2011

Я пытаюсь изменить фон рамки на левой кнопке мыши вниз. Кнопка Я не понимаю. Конечно мой градиент имеет 3 остановки.

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>

Если кто-нибудь увидит ошибку, пожалуйста, сообщите мне;)

1 Ответ

1 голос
/ 27 января 2011

Я не вижу никаких очевидных ошибок, и пример проекта, который я создал с использованием кода, который вы разместили, работает просто отлично. Может быть, вы можете использовать его, чтобы найти источник вашей проблемы

<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>

Обновление

Я также пытался добавить событие MouseLeave, и оно все еще работает.

Обновление 2

Добавлен 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>
...