简体   繁体   中英

Adding controls to a child control during runtime

I'm trying to add a control to another control during runtime. This is what I have so far:

It must be done in .net 3.5

public void addItem(Type addType, Type parentType, string name,string parentName, string fpath)
    {

        try
        {
            if (asdf != null)
            {
            }
            else
            {
                StackPanel stkPnl = (StackPanel)_loadXaml.Content;
                foreach (UIElement child in stkPnl.Children)
                {
                    if ((child.GetType() == parentType))
                    {
                        Control theChild = (Control)child;
                        string theChildsName = theChild.Name;
                        if (theChildsName == parentName)
                        {
                            //I want to create and add the control under "theChild"
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

AddType: is the type of the control
parentType: is the type of the parent of the object that wants to be added
name: is the name of the object that will be added
parentName: is the name of the parent that the created object will be under

I've tried .Children.Add is not an option for "theChild"
also .content is not an option for "theChild"

Is there any way to add the control to its parent during runtime?

Have you tried thisChild.AddVisualChild(controlToAdd)?

See the article here

In Wpf are two main types of controls: the content controls and the items controls. The content controls have only one visual child, and by the other hand the items controls have many visual children. If you want to add at run-time and item into a visual element, I think you must to cast that item to a items control, and then add the item that you want. Hope this helps you to solve your issues...

There are a lot of different variations that could be happening depending on what your UI structure is. Some classes of common elements that might be children of your StackPanel :

  • Decorator - this would most likely be a Border but there are others. Takes 1 child on the Child property.
  • Panel - StackPanel , Grid , Canvas , etc. Can have as many children as you want by adding to the Children property.
  • ContentControl - in addition to using the base class itself also includes things like Button , Label , Expander , many more. Can accept 1 child on the Content property but unlike Decorator.Child this is of Type object and can take anything. The Content that is set is also not a direct visual tree child as with Decorator , but is injected into the control's ControlTemplate . If Content is set to a non-UI element it can also be templated with a DataTemplate .
  • ItemsControl - ListBox , ComboBox , ItemsControl , etc. Similar to ContentControl but takes a collection of content through either the Items or ItemsSource properties (slightly different usage and can only use one or the other). Add children by adding to Items or to whatever collection is bound to ItemsSource .
  • Others that take no children - TextBlock , TextBox , Slider , many others.

There are other possibilities too but these are the most common. Casting to Control isn't getting you anything since those can fall into any of the last 3 categories and misses all of the first 2. You should either define exactly what you're expecting to find in that StackPanel and restrict the types you'll accept as parentType and then cast to one of the first 4 types to set a child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM