views:

5291

answers:

2

Hi,

I want to fade a window in/out in my application. Fading in occurs on Window.Loaded and I wanted to fade out on close (Window.Closed or Window.Closing). Fading in works perfectly but a RoutedEvent is not allowed on Window.Closing. What RoutedEvent should I be using for Close?

<Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:2" FillBehavior="HoldEnd" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Window.Closing">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2" FillBehavior="HoldEnd" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>

I get a error on , Value 'Window.Closing' cannot be assigned to property 'RoutedEvent'. Invalid event name.

Cheers,

James

A: 

I'm not an expert on WPF but I believe that unless you cancel the initial Closing event the window will be gone before the animation is even started.

Upon receiving the Window.Closing event, you should cancel the event and start the animation. When the animation is done you can close the window.

Peter Lillevold
+6  A: 

Closing is not a routed event, so you can't use it in an EventTrigger. Perhaps you could start the storyboard in the handler of the ClosingEvent in the code-behind and cancel the event... something like that :

private bool closeStoryBoardCompleted = false;

private void Window_Closing(object sender, CancelEventArgs e)
{
    if (!closeStoryBoardCompleted)
    {
        closeStoryBoard.Begin();
        e.Cancel = true;
    }
}

private void closeStoryBoard_Completed(object sender, EventArgs e)
{
    closeStoryBoardCompleted = true;
    this.Close();
}
Thomas Levesque
Where do you define the storyboard then?
Slace
You can define it in the resources (but then you can't access it directly in code-behind, you have to get it with FindResource
Thomas Levesque
Dont forget to add <Storyboard Completed="closeStoryBoard_Completed"> to the closing storyboard.
Entrodus