在 WPF 中随时间更改网格投影颜色
本文关键字:网格 投影 颜色 时间 WPF | 更新日期: 2023-09-27 18:37:15
对
WPF 来说有点陌生,总而言之,我想制作一个带有下拉阴影边框的无聊主窗口,在我做某事后会改变其颜色。我想我得到了大部分,只是不知道如何访问网格内的DropShadowEffect。无论如何,这里是XAML
<Window x:Class="Listener.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="500" Loaded="Window_Loaded" WindowStyle="None" AllowsTransparency="True" Background="Transparent" MouseDown="Window_MouseDown" KeyDown="Window_KeyDown">
<Grid Margin="20" Background="White">
<Grid.Effect>
<DropShadowEffect
ShadowDepth="0"
Color="Red"
Opacity="0.9"
BlurRadius="15.0" />
</Grid.Effect>
</Grid>
</Window>
以及相关事件代码
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
ColorAnimation ca = new ColorAnimation(Colors.Red, Colors.Blue, new Duration(TimeSpan.FromSeconds(4)));
Storyboard.SetTarget(ca, ???);
Storyboard.SetTargetProperty(ca, new PropertyPath("Background.Color"));
Storyboard stb = new Storyboard();
stb.Children.Add(ca);
stb.Begin();
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
那么我如何在这个颜色动画中获得阴影效果呢?
你可以
给你的Grid
起个名字,比如RootGrid
<Grid Margin="20" Background="White" x:Name="RootGrid">
<Grid.Effect>
<DropShadowEffect ShadowDepth="0" Color="Red" Opacity="0.9" BlurRadius="15.0"/>
</Grid.Effect>
</Grid>
并更改ColorAnimation
以动画形式显示根网格上Effect
Color
Storyboard.SetTarget(ca, RootGrid);
Storyboard.SetTargetProperty(ca, new PropertyPath("Effect.Color"));
编辑
或者,如果你愿意,也可以在纯 XAML 中实现该效果
。<Grid Margin="20" Background="White">
<Grid.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Blue" Storyboard.TargetProperty="Effect.Color" Duration="0:0:4"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid.Effect>
<DropShadowEffect ShadowDepth="0" Color="Red" Opacity="0.9" BlurRadius="15.0"/>
</Grid.Effect>
</Grid>