Wpf 鼠标悬停时,滑块1的背景色应为暗红色

Wpf 鼠标悬停时,滑块1的背景色应为暗红色,wpf,xaml,Wpf,Xaml,我试图实现幻灯片1的背景色是暗色,当鼠标结束 以下代码需要修复 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width

我试图实现幻灯片1的背景色是暗色,当鼠标结束

以下代码需要修复

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<Slider x:Name="Slider1" Background="Blue" Width="300" Height="30">
    <Slider.Style>
        <Style TargetType="{x:Type Slider}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkOrange" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Slider.Style>
</Slider>
</Window>

我使用事件触发器实现了同样的功能

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Storyboard x:Key="OnMouseEnter">
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                          Storyboard.TargetName="Slider1">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="DarkOrange" />
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="OnMouseLeave">
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                          Storyboard.TargetName="Slider1">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="Orange" />
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseEnter"
                      SourceName="Slider1">
            <BeginStoryboard Storyboard="{StaticResource OnMouseEnter}" />
        </EventTrigger>
        <EventTrigger RoutedEvent="UIElement.MouseLeave"
                      SourceName="Slider1">
            <BeginStoryboard Storyboard="{StaticResource OnMouseLeave}" />
        </EventTrigger>
    </Window.Triggers>
    <Slider x:Name="Slider1"
            Background="Orange"
            Width="300"
            Height="30">

    </Slider>
</Window>