简体   繁体   English

WPF:如何在动态创建的 WPF 窗口中动态添加控件

[英]WPF: How to dynamically Add Controls in dynamically created WPF Window

I want to add a WPF Input Box in my Project in C#.我想在我的 C# 项目中添加一个 WPF 输入框。 I got a WinForm one from InputBox in C# but it has Winform look and feel.我从C# 中的 InputBox获得了一个 WinForm,但它具有 Winform 的外观和感觉。 So i was recreating it in WPF.所以我在 WPF 中重新创建它。 I have created all the controls (Label, Button, Textbox) but i am unable to add them to my window.我已经创建了所有控件(标签、按钮、文本框),但我无法将它们添加到我的窗口中。

static Window winInputDialog

The Window is showing through ShowDialog but without controls.窗口通过 ShowDialog 显示,但没有控件。

There are two ways to get controls in your window:有两种方法可以在窗口中获取控件:

  1. Do the whole designing stuff in the Designer of VisualStudio在 VisualStudio 的设计器中完成整个设计工作
  2. Add the controls by code.通过代码添加控件。 Here is a short, simple sample of creating a window and putting controls in it:下面是一个创建窗口并在其中放置控件的简短示例:

     var window = new Window(); var stackPanel = new StackPanel { Orientation = Orientation.Vertical }; stackPanel.Children.Add(new Label { Content = "Label" }); stackPanel.Children.Add(new Button { Content = "Button" }); window.Content = stackPanel;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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