在 WPF 中随时间更改网格阴影颜色
Change grid dropshadow color over time in WPF
对 WPF 有点陌生,总而言之,我想制作一个无边框的主体 window,其阴影边框会在我执行某些操作后更改其颜色。我想我得到了大部分我只是不知道如何访问网格内的 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();
}
那么如何在这个 ColorAnimation 中获得 dropshadoweffect?
你可以给你的 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
以对 RootGrid
上的 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>
对 WPF 有点陌生,总而言之,我想制作一个无边框的主体 window,其阴影边框会在我执行某些操作后更改其颜色。我想我得到了大部分我只是不知道如何访问网格内的 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();
}
那么如何在这个 ColorAnimation 中获得 dropshadoweffect?
你可以给你的 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
以对 RootGrid
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>