Is there a concept of a master page (like in asp.net) for WPF?

与世无争的帅哥 提交于 2019-12-23 09:01:14

问题


I've started a WPF (web-based) application and it has multiple pages. I want to implement the concept of a Master page in WPF, so that when I make changes in the design/layout of one page then all the others pages will follow suit without all the copy and paste.


回答1:


I don't think the concept of Master Page is valid in WPF basically because there are no pages. In WPF exists styles, templates and DataTemplates. Not only that, using the DockPanel and other containers you can achieve most of the functionality provided by ASP.NET Master Pages.

In any case, if you still the need of the master pages, I suggest check this article: http://www.codeproject.com/KB/WPF/WPFMasterPageControl.aspx

HTH




回答2:


The WPF Magellan Framework (open source) have Master pages (and other several interesting concepts)




回答3:


use a master view and put a ContentControl with the master view, then bind the Content property to a property on your data context and set the property to an instance of a UserControl which will then populate the content section.

I will provide some more detail later




回答4:


You can use global styles to apply a consistent look and feel across your app. As for functionality of a master page. Look at the MVVM pattern. Microsoft has an implementation but there are others.

It let's you define a shell in xaml and specify regions that act sort of like content placeholder's in ASP.NET. Then you write user controls that get loaded into those regions in the shell. It's a lot more involved than using a master page with ASP.NET though. But conceptually similar as far as screen layout goes.

As always, not a bad place to start getting an overview http://en.wikipedia.org/wiki/MVVM

Tons more stuff on MSDN and so on.




回答5:


You can use Prism open source framework (downloadable from: patterns & practices: Prism) and using that you can create a Shell Window, which can be used as the MainWindow for your WPF project. Then, inside your Shell window, you can create Regions and inject your views into those regions.

Below is what MSDN has to say about Shell (See: Composing the User Interface using Prism):

The shell is the application root object that contains the primary UI content. In a Windows Presentation Foundation (WPF) application, the shell is the Window object. In a Silverlight application, the shell is the RootVisualUserControl. The shell plays the role of a master page providing the layout structure for the application. The shell contains one or more named regions where modules can specify the views that will appear. It can also define certain top-level UI elements, such as the background, main menu, and toolbar. The shell defines the overall appearance of the application. It might define styles and borders that are present and visible in the shell layout itself, and it might also define styles, templates, and themes that will be applied to the views that are plugged into the shell. Typically, the shell is a part of the WPF application project or primary Silverlight project. The assembly that contains the shell might or might not reference the assemblies that contain the views to be loaded in the shell's regions.

Basic Prism Shell structure with WPF:



来源:https://stackoverflow.com/questions/5358733/is-there-a-concept-of-a-master-page-like-in-asp-net-for-wpf

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