Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add items to popup programmatically

Tags:

c#

wpf

popup

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
like image 859
Sturm Avatar asked Dec 26 '22 22:12

Sturm


2 Answers

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();
like image 50
peter Avatar answered Jan 11 '23 23:01

peter


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;
like image 29
John Giannetti Avatar answered Jan 11 '23 23:01

John Giannetti