主窗口内的 C# WPF 子窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30407331/
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
C# WPF Child Windows inside Main Window
提问by SuperFlous
So iv looked around for a bit and found out that MDI is obselete for WPF, Basically what i am trying to do is show a specific page in a grid object on load, and once a menu item from my drop down menu is selected, the content of the grid will be changed to the content from a different page (this is depending on which menu item is selected).
因此,iv 环顾四周,发现 MDI 对于 WPF 已过时,基本上我想做的是在加载时在网格对象中显示特定页面,一旦选择了下拉菜单中的菜单项,网格的内容将更改为来自不同页面的内容(这取决于选择的菜单项)。
To go into more detail (perhaps this will help) The area where the window will be shown will need to have the window with no borders, or titles, or buttons to minimize/close etc.. only showing the content of this window, it won't be resizeable but fixed, i have a menu of which as i said earlier, when a different menu item is clicked, the relevant window should be displayed in the fixed area. Additionally if any buttons or events inside this content that is displayed happen (i.e a button causes a different window to show for example) then the content in the fixed area should be replaced by this new window's content
要更详细地(也许这会有所帮助)将显示窗口的区域需要具有没有边框、标题或最小化/关闭按钮等的窗口。仅显示此窗口的内容,它不能调整大小但固定,我有一个菜单,正如我之前所说,当单击不同的菜单项时,相关窗口应显示在固定区域中。此外,如果显示的内容中的任何按钮或事件发生(例如,按钮导致显示不同的窗口),则固定区域中的内容应替换为此新窗口的内容
This is the first time i have done something like this and from what i've read it sounds like this is something very tricky for a WPF application, I hope i can get some sort of insight or direction i should be going so that i can make this possible.
这是我第一次做这样的事情,从我读过的内容来看,这对于 WPF 应用程序来说听起来很棘手,我希望我能得到一些我应该去的洞察力或方向,以便我可以使这成为可能。
Thanks.
谢谢。
回答by Jofta
You can try for example ChildWindowfrom Extended WPF Toolkit Community Edition.
例如,您可以尝试来自 Extended WPF Toolkit Community Edition 的ChildWindow。
Edit #1:
编辑#1:
But whenever i try to create a WindowContainer in the Xaml it has problems with the namespace prefix with "xctk:WindowContainer" so how do i create the appropriate namespace prefix to use it?
但是,每当我尝试在 Xaml 中创建 WindowContainer 时,它都会遇到带有“xctk:WindowContainer”的命名空间前缀的问题,那么我该如何创建适当的命名空间前缀来使用它呢?
You have to add that namespace:
您必须添加该命名空间:
xmlns:xctk=http://schemas.xceed.com/wpf/xaml/toolkit
For example:
例如:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xctk:WindowContainer>
<xctk:ChildWindow Height="100" Width="250" Left="10" Top="10">
<TextBlock Text="Hello World ..." />
</xctk:ChildWindow>
</xctk:WindowContainer>
</Grid>
</Window>
Edit #2:
编辑#2:
You can of course change some properties (for example):
您当然可以更改一些属性(例如):
<xctk:ChildWindow
Height="100"
Width="250"
Left="10"
Top="10"
Name="chWindow"
CloseButtonVisibility="Hidden"
WindowStyle="None"
BorderThickness="0">
Edit #3:
编辑#3:
Ok yeah, so with everything referenced it is giving me errors still..
好的,所以引用的所有内容仍然给我错误..
Try it simple… Create Wpf Application, add Extended WPF Toolkit 2.4 NuGet package, in MainWindow.xaml add previous code and in MainWindow.xaml.cs add next code:
尝试简单……创建 Wpf 应用程序,添加扩展 WPF 工具包 2.4 NuGet 包,在 MainWindow.xaml 中添加之前的代码并在 MainWindow.xaml.cs 中添加下一个代码:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.chWindow.Show();
}
}
}

