Changing content of Window (WPF)

后端 未结 2 447
执笔经年
执笔经年 2021-02-08 08:25

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

2条回答
  •  醉梦人生
    2021-02-08 09:04

    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!

    
        
    
    
     
    
     
    

    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;
    

提交回复
热议问题