Changing content of Window (WPF)

人走茶凉 提交于 2020-01-01 02:29:19

问题


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


回答1:


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.

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!

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

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

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

In code behind of Window:

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

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



回答2:


I use ContentPresenter for snapping in content. In the window, I put something like this:

<ContentPresenter Content="{Binding MainContent}" />

In the view model, I have a property called MainContent of type object:

public object MainContent { get { return (object)GetValue(MainContentProperty); } set { SetValue(MainContentProperty, value); } }
public static readonly DependencyProperty MainContentProperty = DependencyProperty.Register("MainContent", typeof(object), typeof(SomeViewModel), new FrameworkPropertyMetadata(null));

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:

<DataTemplate DataType="{x:Type viewmodels:PlanViewModel}">
    <views:PlanView />
</DataTemplate>

I put that data template in some central resource dictionary along with a bunch of other view-model-to-view mappers.



来源:https://stackoverflow.com/questions/5909441/changing-content-of-window-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!