Rendering not completed in loaded event

I subscribed to the wpf window. Loaded event: Loaded += loaded;and try to change the opacity of some controls in the code. I noticed that in the method the loadedcontrols are not yet colored by wpf. Thus, the code does not work, the rendering of controls occurs only after the method exits.

1) Is there another event, for example. RenderedI can subscribe to?

EDIT: I just discovered that there is an OnContentRendered event, and the following code works:
Although animation is probably preferable.

protected override void OnContentRendered(EventArgs e)
{
   base.OnContentRendered(e);
   for (int i = 0; i < 100; i++)
   {
       Parentpanel.Opacity += 0.01;
       Splashscreen.Opacity -= 0.01;
       Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
       Thread.Sleep(50);
   }
}

Otherwise, I probably have to use an animation that changes the opacity of usercontrol1 from 0.1 to 1.0 and usercontrol2 from 1.0 to 0.0.

2) Do you know an example of such an animation?

+4
2

Loaded (, void ChangeOpacity()) :

Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(ChangeOpacity));

.

, , . XAML, , Blend:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <Window.Resources>
        <Storyboard x:Key="myStoryboard">
            <DoubleAnimationUsingKeyFrames
                         Storyboard.TargetProperty="(UIElement.Opacity)"
                         Storyboard.TargetName="myControl">
                <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource myStoryboard}"/>
        </EventTrigger>
    </Window.Triggers>
    <StackPanel>
        <TextBox x:Name="myControl" Text="I'm disappearing..." />
    </StackPanel>
</Window>
+8

WPF . . "VisibleFader", , . , .

public static DoubleAnimation da;
public static void VisibleFader(FrameworkElement fe)
{
   if (da == null)
   {
      da = new DoubleAnimation();
      da.From = 0;
      da.To = 1;
      da.Duration = new Duration(TimeSpan.FromSeconds(.7));
   }

   fe.IsVisibleChanged += myFader;
}

private static void myFader(object sender, DependencyPropertyChangedEventArgs e)
{
   ((FrameworkElement)sender).BeginAnimation(FrameworkElement.OpacityProperty, da);
}

, (, ), userControl.

MySingletonClass.VisibleFader( this.whateverUserControl );

... , , . - , .

+1

All Articles