How do I load user controls dynamically?

后端 未结 3 1198
清酒与你
清酒与你 2020-12-09 11:00

How can I load a user control[s] in a window dynamically (using code at runtime)?

3条回答
  •  时光说笑
    2020-12-09 11:19

    For adding multiple controls you need container.

    Suppose you have a StackPanel container "myStack"

    
        
    
    

    You can create control dynamically and add it to container. See code below

    void AddButtons()
    {
        Button B1=new Button(),B2=new Button(), B3=new Button();
        B1.Content="Hello";
        B2.Content="First";       
        B3.content="Application";
       // Now you can set more properties like height, width, margin etc...
        MyStack.Children.Add(B1);
        MyStack.Children.Add(B2);
        MyStack.Children.Add(B2);    
    }
    

提交回复
热议问题