wpf 以编程方式将项目添加到弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18163096/
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
Add items to popup programmatically
提问by Sturm
I need to create and add some items to a popup code-behind. It's easy to do this in XAML:
我需要创建一些项目并将其添加到弹出代码隐藏中。在 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
回答by peter
Popup is a FrameworkElement and can have only one child (Child) => you cannot add multiple controls inside, but you can setChild to be any UIElement you want. For instance a DockPanel, and than use AddChild on the panel to add further controls.
Popup 是一个 FrameworkElement 并且只能有一个子元素(Child) => 你不能在里面添加多个控件,但是你可以将Child设置为你想要的任何 UIElement。例如 DockPanel,然后在面板上使用 AddChild 添加更多控件。
myPopup.Child = new DockPanel();
回答by John Giannetti
You would set the child of the PopUp to the DockPanel and then add children to the DockPanel.
您可以将 PopUp 的子项设置为 DockPanel,然后将子项添加到 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;

