【问题标题】:How to animate or add transition to a label's foreground color change?如何为标签的前景色变化设置动画或添加过渡?
【发布时间】:2019-10-28 19:18:45
【问题描述】:

这是针对 WPF 应用程序的。我有一个带有一些文字的标签。我想要的是当鼠标悬停在标签上时,文本的颜色会慢慢变成另一种颜色。当鼠标移开时,颜色会慢慢变回原来的颜色。

我在 MainWindow.xaml 中的代码:

     <Label
            x:Name="mLabel"
            Height="32.446"
            Margin="18.339,65.5,0,0"
            Padding="5,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Content="Hello"
            FontSize="36"
            FontWeight="Bold"
            Foreground="#19418D"
            MouseEnter="MLabel_MouseEnter"
            MouseLeave="MLabel_MouseLeave"
            MouseLeftButtonUp="MLabel_MouseLeftButtonUp"
            RenderTransformOrigin="0.5,0.5"
            Style="{StaticResource CustomFont}"
            UseLayoutRounding="False">
            <Label.RenderTransform>
                <TransformGroup>
                    <ScaleTransform />
                    <SkewTransform />
                    <RotateTransform Angle="360.086" />
                    <TranslateTransform />
                </TransformGroup>
            </Label.RenderTransform>
        </Label>

我在 MainWindow.xaml.cs 中的代码:

private void MLabel_MouseEnter(object sender, MouseEventArgs e)
{
    mLabel.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#7BA8FE"));
}

private void MLabel_MouseLeave(object sender, MouseEventArgs e)
{
    mLabel.Foreground = (SolidColorBrush)(new BrushConverter().ConvertFrom("#19418D"));
}

现在,它是从深蓝色到浅蓝色的瞬间变化。但我希望这种变化在几秒钟内慢慢发生。

这个问题是不同的,因为我在这里使用标签控件的前景色,但另一个解决方案是按钮的背景色,它有不同的实现方式。

【问题讨论】:

  • 您不应该在代码中修改 XAML,xaml 可以自行修改,无需任何代码隐藏
  • 你有没有尝试过?从有关 WPF 中动画的教程开始。不会花很长时间,但您无需询问他人即可解决类似的简单任务。
  • 这是一个矩形,但经过一些修改会达到预期的效果c-sharpcorner.com/Resources/927/…
  • @Sinatr 是的,我在搜索时找到了该页面,但无法使其正常工作。例如,我尝试实现 因为我的是标签而不是按钮,但它不起作用。

标签: c# wpf


【解决方案1】:

使用这个例子Label Hover

创建一个新项目,然后在你的窗口标题之后

Title="MainWindow" Height="450" Width="800">

粘贴以下内容

    <Window.Resources>
        <Style x:Key="MyLabel">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
                                <ColorAnimation To="#7BA8FE" Duration="0:0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
                                <ColorAnimation To="#19418D" Duration="0:0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

从工具箱中拖放屏幕上的任何标签并将其样式属性更改为以下

<Label Content="Label" Style="{DynamicResource MyLabel}" HorizontalAlignment="Left" Margin="335,170,0,0" VerticalAlignment="Top" FontSize="20"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多