简体   繁体   中英

Add items to popup programmatically

I need to create and add some items to a popup code-behind. It's easy to do this in XAML:

<Popup StaysOpen="False">
    <DockPanel>
        //Items here
    </DockPanel>
 </Popup>

I think "Child" is where I need to add my items but I don't see any "Add", "Items", "Source" or "Content" inside. Does anyone know how to do this?

Popup myPopup= new Popup();
myPopup.Child // ... need to add items there

Popup is a FrameworkElement and can have only one child (Child) => you cannot add multiple controls inside, but you can set Child to be any UIElement you want. For instance a DockPanel, and than use AddChild on the panel to add further controls.

myPopup.Child = new DockPanel();

You would set the child of the PopUp to the DockPanel and then add children to the DockPanel.

Here is code that shows that:

   var popup = new Popup();
   var dockPanel = new DockPanel();
   popup.Child = dockPanel;
   dockPanel.Children.Add(new TextBox {Text = "First Child" });
   popup.IsOpen = true;

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