在运行时添加 WPF 控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/311131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:48:32  来源:igfitidea点击:

Add WPF control at runtime

wpf

提问by Frode Lillerud

I've written a WPF UserControl, and want to add one or more of it to my Window at runtime when I click a button. How can I do that?

我已经编写了一个 WPF UserControl,并希望在我单击按钮时在运行时将其中的一个或多个添加到我的窗口中。我怎样才能做到这一点?

Edit: Further specification I want to add the usercontrols to a Canvas, and put in a absolute position. The canvas is a drawing of the floors in my house, and each usercontrol has properties to indicate where in the house it is positioned. So I want all the controls to be positioned in the correct position on the canvas.

编辑:进一步规范我想将用户控件添加到画布,并放在绝对位置。画布是我家地板的绘图,每个用户控件都有属性来指示它在房子中的位置。所以我希望所有控件都位于画布上的正确位置。

I'm thinking something like this

我在想这样的事情

var light = new LightUserControl(2);
HouseCanvas.Children.Add(light); // this should be positioned in a specific place

回答by Ian Oakes

After you add the your control to the Canvas you need to specify the top and left co-ordinates using the Canvas.Top and Canvas.Left attached properties as follows.

将控件添加到 Canvas 后,您需要使用 Canvas.Top 和 Canvas.Left 附加属性指定顶部和左侧坐标,如下所示。

var light = new LightUserControl(2);
HouseCanvas.Children.Add(light);
Canvas.SetLeft(light, 20);
Canvas.SetTop(light, 20);

回答by Ramiro Berrelleza

In case you want to add the control to a Grid instead of a Canvas you can specify all the Grid properties through the Grid static class as follows:

如果您想将控件添加到 Grid 而不是 Canvas,您可以通过 Grid 静态类指定所有 Grid 属性,如下所示:

Label newLabel = new Label();
newLabel.Content = "The New Element";
Main.Children.Add(newLabel);
Grid.SetColumn(newLabel, 0);
Grid.SetRow(newLabel, 0);

回答by Dhruv Singhal

My solution:

我的解决方案:

for (i = 1; i <= buttoncount; i++)
{
    Button mybutton = new Button();
    Grid1.Children.Add(mybutton);
    mybutton.Height = 100;
    mybutton.Width = 100;
    mybutton.Name = "button" + i;
    mybutton.Content = mybutton.Name;
}

回答by Jobi Joy

Add a StackPanel to the window and on each button click,

将 StackPanel 添加到窗口并单击每个按钮,

 _stackPanel.Children.Add(new YourControl());  

You can do this in many ways.

您可以通过多种方式做到这一点。

回答by dexiang

    public static void AddChild(this Visual parent, UIElement child)
    {
        if (InternalAddChild(parent, child))
        {
            return;
        }
        throw new NotSupportedException();
    }
    private static bool InternalAddChild(Visual parent, UIElement child)
    {
        Panel panel = parent as Panel;
        if (panel != null)
        {
            panel.Children.Add(child);
            return true;
        }
        for (int i = VisualTreeHelper.GetChildrenCount(parent) - 1; i != -1; i--)
        {
            Visual target = VisualTreeHelper.GetChild(parent, i) as Visual;
            if (target != null && InternalAddChild(target, child))
            {
                return true;
            }
        }
        return false;
    }

回答by Cassius

Just complementing the answer:

只是补充答案:

for (i = 1; i <= buttoncount; i++)
{
    Button mybutton = new Button();
    Grid1.Children.Add(mybutton);
    mybutton.Height = 100;
    mybutton.Width = 100;
    mybutton.Name = "button" + i;
    mybutton.Content = mybutton.Name;
    mybutton.Click += button_Click;
}

private void button_Click(object sender, RoutedEventArgs e)
{
    // do something
}