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
Add a WPF control to a Windows FormOpen Form1 in the Windows Forms Designer. In the Toolbox, find the tab labeled WPFUserControlLibrary WPF User Controls. Drag an instance of UserControl1 onto the form. An ElementHost control is created automatically on the form to host the WPF control.
A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.
Controls in WPF are accessed by their Name properties. We specify the Name property in the XAML, and then can access the control by that name directly in C# code. Property notes. Name allows controls to interact.
Adding Multiple Controls to the Content Panel | Basic Library for WPF and Silverlight | ComponentOne. You cannot set the Content property to more than one control at a time.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With