繁体   English   中英

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

[英]Change grid dropshadow color over time in WPF

对于WPF来说,这是Kinda的新功能,总而言之,我想制作一个无阴影的主窗口,并带有阴影边框,在我做某事后可以更改其颜色。 我想我已经掌握了其中的大部分内容,但我只是不知道如何访问网格内的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以动画Effect RootGrid的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM