Vajura
Vajura

Reputation: 1132

Change grid dropshadow color over time in WPF

Kinda new to WPF, all in all i wanted to make a bordeless main window with a dropshadow border that changes its color after i do something. I think i got most of it i just dont know how to access the DropShadowEffect inside the Grid. Anyways here is the 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>

And the relevent event code

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();
}

So how do i get the dropshadoweffect inside this ColorAnimation?

Upvotes: 0

Views: 2231

Answers (1)

dkozl
dkozl

Reputation: 33384

You could give your Grid some name, say 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>

and change ColorAnimation to animate Color of the Effect on RootGrid

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

EDIT

Or, if you choose to, you could achieve that effect in pure XAML as well

<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>

Upvotes: 1

Related Questions