鼠标悬停时Slider1的背景色应为DarkOrange

时间:2018-09-08 19:21:05

标签: wpf xaml

我试图在鼠标悬停时实现Slider1的背景色为DarkOrange。

以下代码需要修复。

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

1 个答案:

答案 0 :(得分:0)

我使用事件触发器来实现相同目标。

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