Although WPF dynamically loads controls from the background, it is not often used, but there are not many materials on the Internet. Today, I made such a small thing and recorded it as follows:
First, we add a Dynamic Loading Function loaded = "window_loaded" in the file header of The XAML file"
And name the window form,
X: Name = "win_mainx"
The definition of the entire header is as follows:
<Window X: class = "listing_3. _ about_dialog_with_tree_mongoing.imagewindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "imagewindow" Height = "300" width = "505" loaded = "window_loaded" X: Name = "win_mainx">
In the CS file in the background, we take loading a button dynamically as an example. In the window_loaded function,
As follows:Code:
Canvas ccontent = new canvas ();
// Generate a button
Button mybutton = new button ();
Mybutton. content = "button ";
Mybutton. width = 100;
Mybutton. Height = 50;
Ccontent. Children. Add (mybutton );
Win_mainx.content = ccontent;
If you need to define the control location, we can see this in the XAML file <grid> </GRID>
Add a name, for example, <grid X: Name = "gdmain"> </GRID>
Then, we modify win_mainx.content = ccontent;
Mybutton. Margin = New thickness (10, 10, 10, 10 );
Gdmain. Children. Add (ccontent );
Run. One button is loaded.