更改窗口的内容 (WPF)
我创建了一个简单的 WPF 应用程序,它有两个 Windows。用户在第一个窗口中填写一些信息,然后单击“确定”,这会将他们带到第二个窗口。这工作正常,但我试图将两个窗口合并到一个窗口中,这样只是内容发生了变化。
我设法找到了这个 更改窗口内容时的资源管理,这似乎是我在追求什么。但是,我搜索了 ContentPresenter,但找不到关于如何使用它的太多帮助。例如,如果我使用 ContentPresenter,我应该将两个 Windows 中的现有 XAML 元素放在哪里?我猜第一个窗口将进入 ContentPresenter,但第二个窗口需要放置在某个地方,以便在需要切换时使用。
任何帮助都会很棒。一个简单的工作示例会更好。
TIA
I've created a simple WPF application which has two Windows. The user fills in some information on the first Window and then clicks Ok which will take them to the second Window. This is working fine but I'm trying to incorporate both Windows into a single Window so just the content changes.
I managed to find this Resource management when changing window content which seems like it is what I'm after. However, I've search for ContentPresenter but couldn't find much help for how I need to use it. For example, if I use a ContentPresenter, where do I put the existing XAML elements that are in the two Windows? I'm guessing the first Window will go into the ContentPresenter but the second one will need to be put somewhere for when it needs to be switched in.
Any help would be great. A simple working example would be even better.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ContentPresenter
通常在重新设计现有控件时使用。它是放置控件内容的地方。相反,您应该使用ContentControl
,它只是一个具有内容元素的控件。或者,您可以直接设置窗口的内容。您将两个现有窗口的内容提取到两个用户控件中。然后您创建一个新窗口来托管内容。根据您的业务逻辑,您可以将该窗口的内容(或者如果您想要其他“主”内容,则将该窗口的 ContentControl)设置为这两个 UserControl 中的一个。
编辑:
作为起点。这不是完整的工作代码,只是为了帮助您入门。请注意,这是一个糟糕的架构;一旦运行起来,您可能应该使用 MVVM 或类似的方法!
在Window后面的代码中:
A
ContentPresenter
is normally used when restyling existing controls. It is the place where the Content of a control is placed. Instead you should use aContentControl
, which is simply a control that has a content element. Alternatively, you could directly set the Content of your window.You extract the contents of your two existing windows into two UserControls. Then you create a new Window which will host the contents. Depending on your business logic, you set the content of that window (or that window's ContentControl if you want additional "master" content) to either of those two UserControls.
EDIT:
As a starting point. This is not complete working code, just to get you started. Note that this is bad architecture; you should probably use a MVVM or similar approach once you get this running!
In code behind of Window:
我使用 ContentPresenter 来捕捉内容。在窗口中,我放置了如下内容:
在视图模型中,我有一个名为 MainContent 的对象类型属性:
无论您将 MainContent 设置为什么,都会显示在窗口中。
为了保持视图和视图模型之间的分离,我通常将 MainContent 属性设置为另一个视图模型,并使用数据模板将该视图模型映射到视图:
我将该数据模板与一堆其他资源字典一起放在某个中央资源字典中视图-模型-视图映射器。
I use ContentPresenter for snapping in content. In the window, I put something like this:
In the view model, I have a property called MainContent of type object:
Whatever you set MainContent to will show up in the window.
To keep the separation between view and view model, I typically set the MainContent property to another view model and use a data template to map that view model to a view:
I put that data template in some central resource dictionary along with a bunch of other view-model-to-view mappers.