Dynamically add multiple buttons to wpf window?

前端 未结 3 2019
野的像风
野的像风 2020-12-01 04:17

how would i add multiple buttons to a window in c#? here\'s what i need to do... i\'m getting multiple user values from a dictionary (within reason, only @ 5-6 values). for

3条回答
  •  眼角桃花
    2020-12-01 04:41

    Consider you have a StackPanel named sp

    for(int i=0; i<5; i++)
    {
        System.Windows.Controls.Button newBtn = new Button();
    
        newBtn.Content = i.ToString();
        newBtn.Name = "Button" + i.ToString();
    
        sp.Children.Add(newBtn);
    }
    

    To remove button you could do

    sp.Children.Remove((UIElement)this.FindName("Button0"));
    

    Hope this help.

提交回复
热议问题