Pergunta

I am looking for a way to force a WindowsFormsHost control to redraw itself after an animation is finished moving it, but I am not finding a solution.

I followed an example to have a sliding panel in my WPF application by following http://bitsbobsetc.wordpress.com/2011/07/10/creating-a-sliding-panel-in-wpf/

In the example he uses only basic wpf controls. In my sliding panel the main part of it is the WindowsFormsHost. The issue I am having is the animation happens as it should, but the WindowsFormsHost does not get drawn until after the user interacts with something else. Like a refresh of the main window makes it visible.

The same sort of thing happens when the sliding panel is to be hidden. WindowsFormsHost is still shown and can be interacted with until the user interacts with something else.

From Animating WindowsFormsHost it seems like this is a known issue, but I am not overly concerned with the animation, just showing and hiding the panel.

Below are some images to help describe the current behavior.

SidePanelClosed SidePanelOpenNotDrawn SidePanelOpenAndDrawn SidePanelClosedButStillDrawn

Foi útil?

Solução

Through researching this issue I found that there were some events that were available for the Storyboard class. one of the events is the Completed event.

By tying in information from the other posts of using .InvalidateVisual() which forces a redraw of the WindowsFormsHost. This works for the sliding out portion of the animation. For the sliding back I just simply hid the WindowsFormsHost as it began its animation of closing. This way it would not linger out in the user space while the panel was closing.

Xaml:

<Storyboard x:Key="expandStoryBoard" 
    TargetProperty="RenderTransform.(TranslateTransform.X)"
    AccelerationRatio=".4"
    DecelerationRatio=".4" Completed="Storyboard_Completed">
        <DoubleAnimation Storyboard.TargetName="SidePanel" Duration="0:0:0.6"
    From="0" To="250">
</Storyboard>

Code behind:

private void Storyboard_Completed(object sender, EventArgs e)
{
    this.InvalidateVisual();
}

private void OpenPanel()
{
    SidePanel.BeginStoryboard((Storyboard)this.Resources["expandStoryBoard"]);
    expanded = !expanded;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top