在WPF中随时间更改网格阴影颜色

时间:2015-01-12 11:45:48

标签: c# wpf xaml

有点新的WPF,总而言之,我想制作一个带有阴影边框的无边框主窗口,在我做某事后会改变颜色。我想我得到了大部分内容我只是不知道如何访问Grid中的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中获得drophadoweffect?

1 个答案:

答案 0 :(得分:1)

你可以给你的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上Color的{​​{1}}动画

Effect

修改

或者,如果你选择,你也可以在纯XAML中实现这种效果

Storyboard.SetTarget(ca, RootGrid);
Storyboard.SetTargetProperty(ca, new PropertyPath("Effect.Color"));