Reputation: 316
is there a way to add a user control to a WPF Window created in code? I cant find a Children property in the Window Class. In xaml It would 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 code I tried something like this:
Window myWindow = new Window;
UC1 uc1 = new UC1;
myWindow.Children.Add(UC1);
Thanks for your help
Upvotes: 10
Views: 19948
Reputation: 17658
A Children property is there if you have an ItemsControl, i.e. a control which can have multiple children. A Window is a ContentControl, i.e. it only has one "child", the Content. So the code should be:
myWindow.Content = UC1;
Upvotes: 13