Adding a WPF UserControl to a WPF Window in Code

Is there a way to add a custom control to a WPF window created in code? I cannot find the Children property in the window class. In xaml, it will look like this:

<Window x:Class="MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MyUserControls="clr-namespace:MyUserControls" Title="" Height="Auto" Width="550" ResizeMode="NoResize"> <MyUserControls:UC1 x:Name="uc1" /> </Window> 

In the code, I tried something like this:

 Window myWindow = new Window; UC1 uc1 = new UC1; myWindow.Children.Add(UC1); 

thanks for the help

+8
wpf user-controls
source share
1 answer

And the "Children" property exists if you have an ItemsControl , that is, a control that can have several children. The ContentControl window, that is, it has only one "child", Content . Thus, the code should be:

 myWindow.Content = UC1; 
+9
source share

All Articles