What is the difference between Window.Loaded and Window.ContentRendered events

What is the difference between Window.Loaded and Window.ContentRendered in WPF? Is the ContentRendered event ContentRendered first?

The Window.ContentRendered event Window.ContentRendered here just says

Occurs after rendering the contents of the window.

Window.Loaded event Window.Loaded says here

Occurs when an item is laid out, displayed, and ready for interaction.

I have a case where I want to set the MaxHeight window to the height of the working area of ​​the screen displaying my window. What event should I do?

Edit:

I think I found what I was looking for, but now I'm even more confused. The Loaded event occurs first, and then the ContentRendered event ContentRendered . Chris Cells and Ian Griffiths' WPF Programming Book Says Loaded Event

Raised before window display

While the ContentRendered event

Raised when the visual display of the contents of the window.

This is contrary to what the MSDN documentation says about the Loaded event:

Occurs when an item is laid out, displayed, and ready for interaction.

This is even more confusing.

+45
events wpf window
Aug 26 '13 at 20:47
source share
3 answers

I think that there is little difference between the two events. To understand this, I created a simple manipulation example:

XAML

 <Window x:Class="LoadedAndContentRendered.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MyWindow" Title="MainWindow" Height="1000" Width="525" WindowStartupLocation="CenterScreen" ContentRendered="Window_ContentRendered" Loaded="Window_Loaded"> <Grid Name="RootGrid"> </Grid> </Window> 

Code behind

 private void Window_ContentRendered(object sender, EventArgs e) { MessageBox.Show("ContentRendered"); } private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("Loaded"); } 

In this case, the Loaded message appears first after the ContentRendered message. This confirms the information in the documentation.

In general, in WPF, the Loaded event fires when an element:

Placed, displayed and ready for interaction.

Since in WPF Window is one and the same element, but it should be, as a rule, content located on the root panel (for example: Grid ). Therefore, to control the contents of the Window and to create the ContentRendered event. MSDN Notes:

If there is no content in the window, this event will not be generated.

That is, if we create a Window :

 <Window x:Class="LoadedAndContentRendered.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MyWindow" ContentRendered="Window_ContentRendered" Loaded="Window_Loaded" /> 

It will only work with the Loaded event.

As for accessing elements in Window , they work the same way. Let me create a Label in the main Grid Window . In both cases, we successfully gained access to Width :

 private void Window_ContentRendered(object sender, EventArgs e) { MessageBox.Show("ContentRendered: " + SampleLabel.Width.ToString()); } private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("Loaded: " + SampleLabel.Width.ToString()); } 

As for Styles and Templates , at this stage they are successfully applied, and in these cases we will be able to access them.

For example, we want to add Button :

 private void Window_ContentRendered(object sender, EventArgs e) { MessageBox.Show("ContentRendered: " + SampleLabel.Width.ToString()); Button b1 = new Button(); b1.Content = "ContentRendered Button"; RootGrid.Children.Add(b1); b1.Height = 25; b1.Width = 200; b1.HorizontalAlignment = HorizontalAlignment.Right; } private void Window_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("Loaded: " + SampleLabel.Width.ToString()); Button b1 = new Button(); b1.Content = "Loaded Button"; RootGrid.Children.Add(b1); b1.Height = 25; b1.Width = 200; b1.HorizontalAlignment = HorizontalAlignment.Left; } 

In the event of a Loaded Button event, add to the Grid immediately when Window appears. In the event of a ContentRendered Button event, add to the Grid after its contents appear.

Therefore, if you want to add elements or changes before loading Window , you should use the Loaded event. If you want to perform operations related to the contents of Window , for example, using screenshots, you will need to use the ContentRendered event.

+47
Aug 27 '13 at 6:02
source share

If you follow this link https://msdn.microsoft.com/library/ms748948%28v=vs.100%29.aspx#Window_Lifetime_Events and scroll down to Window Lifetime Events, it will show you the order of events.

Open

  • Sourceinitiated
  • Activated
  • Loaded
  • ContentRendered

Close

  • Closing
  • MMG
  • Closed
+37
Aug 26 '13 at 21:26
source share

If you are using data binding, you need to use the ContentRendered event.

For the code below, the header is NULL when the Loaded event is raised. However, the header gets its value when the ContentRendered event is raised.

 <MenuItem Header="{Binding NewGame_Name}" Command="{Binding NewGameCommand}" /> 
+8
Apr 16 '14 at 1:26
source share



All Articles