10

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

1
  • This is just pseudo code I wrote while typing the question. There is no Children property in the window class. That is my problem Commented May 20, 2011 at 6:49

1 Answer 1

13

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.