WPF dynamic Add control settings style

Source: Internet
Author: User
The new company mainly uses WPF as a project, and has not been exposed to such things before, after a week of learning found that the layout of the WPF page, style settings and web design almost.

Both have style files that can be used to set styles uniformly for controls, even with a similar priority, and find it interesting. Here's how to add controls dynamically after the program is run.

To add a control, the container is required, and several large layout controls can be Grid Canvas uniformgrid dockpanel StackPanel WrapPanel below for an example of WrapPanel

First up code

<summary>

///methods for adding controls

///</summary>

///<param name= "Container" > Pannel class instance </ param>

 ///<param name= "Control" >uielement instance </param> public

 static void Dynamicadd (panel Container, UIElement control)

 {

     Container.Children.Add (control);

 }

 

 private void Buttonadd_click (object sender, RoutedEventArgs e)

 {

 

     button btn = New button ();

     Btn. Width =;

     Btn. Height =;

     Set a button style Use this method you must refer the resource file to the current page

     btn. Style = resources["Noticebutton"] as Style;

     This method can also set styles

     //Get styles in App.xaml personally prefer this

     //style style = (style) this. FindResource ("Noticebutton");

     Sets the style

     //btn for the button. style = style;

     Add btn to UFG

     dynamicadd (WRP, BTN);

 


This is the background code is very simple only when you set the style to pay attention to which method

Foreground XAML code is primarily a reference to a resource

<window.resources> <ResourceDictionary> <resourcedictionary.mergedd ictionaries> <resourcedictionary source= "/css/eventsbutton.xaml"/> </resourcedict Ionary. mergeddictionaries> </ResourceDictionary> </Window.Resources> <Grid> <grid.row definitions> <rowdefinition ></RowDefinition> <rowdefinition height= "><" /rowdefinition> </Grid.RowDefinitions> <wrappanel name= "WRP" grid.row= "0" ></wrappanel>
        ; <button content= "Add" grid.row= "1" height= "horizontalalignment=" Center "name=" Buttonadd "verticalalignment=" Top "width=" click= "Buttonadd_click"/> </Grid> 

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.