将窗口控件添加为视觉对象的 WPF 解决方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30739562/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:11:46  来源:igfitidea点击:

WPF Workaround to add Window control as a child of visual

c#wpfuser-interface

提问by user94592

I have a requirement to add a Window control inside a panel.

我需要在面板内添加 Window 控件。

Of course this doesn't seem possible: Window must be the root of the tree. Cannot add Window as a child of Visual.

当然,这似乎不可能:Window 必须是树的根。无法将 Window 添加为 Visual 的子项。

The problem boils down to drawing the Window Chrome (control box, title bar, minimize/restore/close button and borders) inside a panel using the OS theme configured by the user. Are there any workarounds I should be investigating?

问题归结为使用用户配置的操作系统主题在面板内绘制 Window Chrome(控制框、标题栏、最小化/恢复/关闭按钮和边框)。是否有任何我应该调查的解决方法?

回答by Dan Field

This is what User Controls are for - creating controls that can be reused in multiple windows. Refactor your Window Control so that it is just a Window that contains a single User Control (which in turn contains everything your window previously had). Place the User Control in your Panel. The idea is that your current window (that you want to be a child, we'll call it Window B) looks something like this:

这就是用户控件的用途 - 创建可以在多个窗口中重复使用的控件。重构您的窗口控件,使其只是一个包含单个用户控件的窗口(它又包含您的窗口以前拥有的所有内容)。将用户控件放在您的面板中。这个想法是你当前的窗口(你想成为一个孩子,我们称之为窗口 B)看起来像这样:

Window B -> Grid/Panel -> Other Controls

Window B -> Grid/Panel -> Other Controls

And should instead look like this

而是应该看起来像这样

Window B -> Grid/Panel -> User Control B -> Grid/Panel -> Other Controls

Window B -> Grid/Panel -> User Control B -> Grid/Panel -> Other Controls

And now other other window can look like this:

现在其他窗口看起来像这样:

Window A -> Grid/Panel -> ... -> Panel -> User Control B

Window A -> Grid/Panel -> ... -> Panel -> User Control B



If you're trying to achieve an MDI like interface, you have a few options - use tab controls, use a panel that can be dragged and/or resized, etc. In this case, you would stillwant a User Control, so that it could easily be reused in different windows (or panels) that you create, especially if you end up having to create those panels programatically (e.g. if you intend to create multiple instances dynamically at runtime).

如果您正在尝试实现类似 MDI 的界面,您有几个选项 - 使用选项卡控件、使用可以拖动和/或调整大小的面板等。在这种情况下,您仍然需要一个用户控件,以便它可以很容易地在您创建的不同窗口(或面板)中重用,特别是如果您最终必须以编程方式创建这些面板(例如,如果您打算在运行时动态创建多个实例)。

In this case, you'll probably want twouser controls: one that has all the controls from your other window, and one that would act as a "window" control within your main window. The "Window" control would have functionality for resizing, docking, etc.

在这种情况下,您可能需要两个用户控件:一个包含其他窗口中的所有控件,另一个用作主窗口中的“窗口”控件。“窗口”控件将具有调整大小、停靠等功能。

WPF does not support creating a window as if it were a control, or as a child of another window (the closest you can get to that is having a window be shown as a modal dialog).

WPF 不支持创建一个窗口,就好像它是一个控件,或者作为另一个窗口的子窗口(最接近的是让窗口显示为模式对话框)。

回答by user94592

As the only answer is completely off topic, here is the best answer I can come up with: Create window, render window as Bitmap, put bitmap inside panel.

由于唯一的答案完全偏离主题,这是我能想到的最佳答案:创建窗口,将窗口渲染为位图,将位图放入面板中。