« PDC 08 has been announced | Main | A WPF desktop alert control »

Sunday, December 16, 2007

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8342fe72c53ef00e54fa64bc58833

Listed below are links to weblogs that reference Closing a WPF custom control from a storyboard:

Comments

Charlie

Nicke, I've found a way to do this. It's a bit of a chore, but I think allowing people to change the open/close animations without subclassing is most important.

Nicke

I haven't had time to work on this for a while, so I haven't explored the issue further. If you've found a solution to this, could you share it?

Andreas

If you put the fading into the control, not in its template you will be able to access the methods of the class representing the control. So you will be able to handle the Completed event. You may also read the corresponding article in the documentation to be able to ensure that the Completed event is raised when you want it.

Nicklas Andersson

The problem here was that I wanted the control tom be templateable.

Andreas

Sorry, I did not notice that the whole animation had to be part of the template in your case. Of course it does only make sense if the whole control is animated.
VS 2008 creates the event handlers, even if the were not accessible. Perhaps any settings regarding filling (FillBehavior) may resolve your problem - I really don't know, sorry.

Parx

I recently ran into this same problem - animating a window close, or more specifically, executing a command from a storyboard. I achieved this using the attached property (the cure-all for bridging xaml and object models without code-behind, so I'm told). At the end of the close storyboard, I added the following timeline (note that xml markup was 'marked up' for sake of posting):
[ObjectAnimationUsingKeyFrames BeginTime="00:00:03" Storyboard.TargetName="closeButton" Storyboard.TargetProperty="(behaviors:StoryboardCompletedCommandExecutor.ExecuteCommand)" Duration="00:00:00.0010000"]
[DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static ApplicationCommands.Close}"/]
[/ObjectAnimationUsingKeyFrames]

The button which triggers the animation contains standard xaml with the optional CommandParameter attribute normally set to {Binding}. Note that the Command attribute is removed otherwise you get the standard behavior of the window immediately closing with no animation.

And for the attached property (which as you may have noticed above we call behaviors), the PropertyChangedCallback is coded as:
private static void ExecuteCommandChanged( DependencyObject depObj, DependencyPropertyChangedEventArgs args )
{
RoutedUICommand command = ApplicationCommands.NotACommand;
if ( args.NewValue is RoutedUICommand )
{
command = (RoutedUICommand)args.NewValue;
}

if ( ( command != null ) && ( command != ApplicationCommands.NotACommand ) )
{
FrameworkElement target = depObj as FrameworkElement;
if ( target != null )
{
object parameter = null;
ButtonBase button = depObj as ButtonBase;
if ( button != null )
{
parameter = button.CommandParameter;
}
if ( command.CanExecute( parameter, target ) )
{
command.Execute( parameter, target );
}
}
}
}

The code was written specifically for our need at the time, so perhaps it could be genericized. Our close is triggered by a close button, so special casing was added to pull the command parameter from the source button if it is available.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment