2017-08-16 96 views
0

我正在創建一個WPF應用程序。方法:單一窗口應用程序,如web SPA應用程序,帶有菜單,頁眉,頁腳和頁面內容部分。然後,在頁面中,我應該至少定義頁面內容,可選地添加一些額外的菜單,頁腳。就像ASP.NET中的Master Pages一樣。WPF如何全球佈局

是否有任何最佳實踐,如在MasterPage中的ASP.NET,如何實現這一目標?我想尊重DRY,並且不要在每個頁面中定義具有菜單,標題等的網格。

謝謝。

+0

您通常只會有一個固定設計的窗口,然後換出內部組件來改變您正在查看的內容。母版頁對於WPF來說並不合適,因爲您不會導航到其他需要重新整體佈局的地方。 – poke

+0

MVVM將做... ContentControl/ItemsControl +數據模板。 – Sinatr

回答

0

WPF,不支持使用母版頁。最初,ASP.Net是用來創建Web應用程序的一種更簡單的方法,就像您創建WPF應用程序的方式一樣,只是具有更多種選項,比如母版頁。

但是,有一些最佳實踐可以在自定義控件的幫助下在WPF中實現此目的。

例如:一個包含標題摘要和內容的自定義控件。

namespace MasterPages.Master 
{ 
    public class Master : Control 
    { 
    static Master() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(Master), 
     new FrameworkPropertyMetadata(typeof(Master))); 
    } 

    public object Title 
    { 
     get { return (object)GetValue(TitleProperty); } 
     set { SetValue(TitleProperty, value); } 
    } 

    public static readonly DependencyProperty TitleProperty = 
     DependencyProperty.Register("Title", typeof(object), 
     typeof(Master), new UIPropertyMetadata()); 

    public object Abstract 
    { 
     get { return (object)GetValue(AbstractProperty); } 
     set { SetValue(AbstractProperty, value); } 
    } 

    public static readonly DependencyProperty AbstractProperty = 
     DependencyProperty.Register("Abstract", typeof(object), 
     typeof(Master), new UIPropertyMetadata()); 

    public object Content 
    { 
     get { return (object)GetValue(ContentProperty); } 
     set { SetValue(ContentProperty, value); } 
    } 

    public static readonly DependencyProperty ContentProperty = 
     DependencyProperty.Register("Content", typeof(object), 
     typeof(Master), new UIPropertyMetadata()); 
    } 
} 

完整的例子是:

https://www.codeproject.com/Articles/23069/WPF-Master-Pages

+1

謝謝你的答案。我已經按照你的回答,結合這篇文章:https://stackoverflow.com/questions/18140937/multiple-content-presenters-in-a-wpf-user-control 這解決了我的問題,似乎是一個很好的解決方案如何保持WPF應用程序乾燥和良好的維護 – Luke1988