窗口的更改内容(WPF) [英] Changing content of Window (WPF)

查看:189
本文介绍了窗口的更改内容(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了有两个窗口一个简单的WPF应用程序。用户填写的第一个窗口中的一些信息,然后单击确定将带他们到第二个窗口。这是工作的罚款,但我想在Windows纳入一个单一的窗口所以只是内容的变化

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.

我设法找到这个的Resource管理改变这好像是我后窗口内容时。不过,我搜索ContentPresenter但无法找到我需要如何使用它太大帮助。例如,如果我使用了一个ContentPresenter,我在哪里把那些在两个Windows现有的XAML元素?我猜第一个窗口将进入ContentPresenter,但第二个将需要投入的地方为需要进行切换时。

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

推荐答案

A ContentPresenter 改型现有的控制措施时,正常使用。它是在那里的控制的内容被放置的地方。相反,你应该使用 ContentControl中,这简直是一个具有内容元素的控制。或者,你可以直接设置窗口中的内容。

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 a ContentControl, which is simply a control that has a content element. Alternatively, you could directly set the Content of your window.

您提取您现有的两个窗口的内容分为两个用户控件。然后创建一个新的窗口,将举办内容。根据您的业务逻辑,设置窗口中的内容(或窗口的ContentControl中,如果你想要更多的主人的内容)来完成这两个用户控件中。

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.

编辑

作为一个起点。这不是完整的工作代码,只是为了让你开始。请注意,这是不好的架构; !你应该使用MVVM或类似的方法,一旦你得到这个运行

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!

<Window>
    <ContentControl Name="ContentHolder" />
</Window>

<UserControl x:Class="MyFirstUserControl" /> <!-- Originally the first window -->

<UserControl x:Class="MySecondUserControl" /> <!-- Originally the second window -->

在窗口后面的代码:

// Somewhere, ex. in constructor
this.ContentHolder.Content = new MyFirstUserControl;

// Somewhere else, ex. in reaction to user interaction
this.ContentHolder.Content = new MySecondUserControl;

这篇关于窗口的更改内容(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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