How to create a main layout template in XAML

前端 未结 1 1859
北海茫月
北海茫月 2020-12-18 10:07

I\'m new to windows development. I\'m making a simple windows app which has a few pages and each page has a similar layout in XAML. Like this:

Each page is

1条回答
  •  悲&欢浪女
    2020-12-18 10:59

    One feasible way to do this is using UserControl with ContentPresenter. For example:

    Add a UserControl named MainTemplate. In the XAML, set the layout with ContentPresenter and bind it to the DependencyProperty defined in code-behind.

    
    
        
            
                
                
                
            
    
            
    
            
    
            
        
    
    

    In the code-behind, set the DependencyProperty so that we can use them to set the content in other pages.

    public sealed partial class MainTemplate : UserControl
    {
        public MainTemplate()
        {
            this.InitializeComponent();
        }
    
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));
    
        public object Title
        {
            get { return GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
    
        public static readonly DependencyProperty MainProperty = DependencyProperty.Register("Main", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));
    
        public object Main
        {
            get { return GetValue(MainProperty); }
            set { SetValue(MainProperty, value); }
        }
    
        public static readonly DependencyProperty StuffProperty = DependencyProperty.Register("Stuff", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));
    
        public object Stuff
        {
            get { return GetValue(StuffProperty); }
            set { SetValue(StuffProperty, value); }
        }
    }
    

    After this, we can use the UserControl in other pages to reuse the general layout. For example, using it in "MainPage.xaml":

    
    
        
            
                
                    A
                
            
            
                
                    B
                
            
            
                
                    C
                
            
        
    
    

    Then the "MainPage" will look like follwoing:

    0 讨论(0)
提交回复
热议问题