WPF:如何在动态创建的 WPF 窗口中动态添加控件 [英] WPF: How to dynamically Add Controls in dynamically created WPF Window

查看:51
本文介绍了WPF:如何在动态创建的 WPF 窗口中动态添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 C# 项目中添加一个 WPF 输入框.我从 C# 中的 InputBox 获得了一个 WinForm,但它具有 Winform 的外观和感觉.所以我在 WPF 中重新创建它.我已经创建了所有控件(标签、按钮、文本框),但我无法将它们添加到我的窗口中.

I want to add a WPF Input Box in my Project in C#. I got a WinForm one from InputBox in C# but it has Winform look and feel. So i was recreating it in WPF. I have created all the controls (Label, Button, Textbox) but i am unable to add them to my window.

static Window winInputDialog

窗口通过 ShowDialog 显示,但没有控件.

The Window is showing through ShowDialog but without controls.

推荐答案

有两种方法可以在窗口中获取控件:

There are two ways to get controls in your window:

  1. 在 VisualStudio 的设计器中完成整个设计工作
  2. 通过代码添加控件.下面是一个创建窗口并在其中放置控件的简短示例:

  1. Do the whole designing stuff in the Designer of 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;

这篇关于WPF:如何在动态创建的 WPF 窗口中动态添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆