WPF Dynamic Loading Control

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.