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.