以编程方式向弹出窗口添加项目

3

我需要在代码后台创建并添加一些项目到弹出窗口。在XAML中很容易实现:

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

我认为“Child”是我需要添加项目的地方,但我没有看到任何“添加”,“项目”,“来源”或“内容”。有人知道如何做吗?

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

2
你尝试过将StackPanel设置为Child,然后向该StackPanel添加项吗? - fex
要添加的项目始终相同吗?也就是说,它们都是同一种控件,只是数据不同吗?还是说在运行时决定完全不同的控件? - Alejandro
2个回答

5
您需要将弹出窗口的子元素设置为 DockPanel,然后将子元素添加到 DockPanel 中。
以下是示例代码:
```html ```
   var popup = new Popup();
   var dockPanel = new DockPanel();
   popup.Child = dockPanel;
   dockPanel.Children.Add(new TextBox {Text = "First Child" });
   popup.IsOpen = true;

5

弹出窗口是一个FrameworkElement,只能有一个子元素(Child)=> 你不能在里面添加多个控件,但可以设置Child为任何你想要的UIElement。例如DockPanel,然后使用AddChild在面板上添加更多控件。

myPopup.Child = new DockPanel();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接