c# - XAML Reuse a block of markup -


consider have multiple pages , want show same "header" in every page without copying & pasting multiple times: it's unmaintainable.

    <!--  #region header  -->      <grid grid.row="0">          <rectangle style="{staticresource headerbackground}" />         <textblock                     style="{staticresource pagetitle}"                    text="page1" />         <button                  content="settings"                 command="{binding gotosettingspage}" />      </grid>      <!--  #endregion header  --> 

what options make block of markup reusable? example nice write this:

<myheader grid.row="0" pagetitle="page1" /> 

a user control option.

myheader.xaml:

<usercontrol     x:class="app1.myheader"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="using:app1"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:ignorable="d"     d:designheight="300"     d:designwidth="400">      <grid>         <rectangle style="{staticresource headerbackground}" />         <textblock x:name="title"                    style="{staticresource pagetitlestyle}"                    text="" />         <button content="settings"                 command="{binding gotosettingspage}"/>     </grid> </usercontrol> 

myheader.xaml.cs:

using windows.ui.xaml.controls;  namespace app1 {     public sealed partial class myheader : usercontrol     {         public myheader()         {             this.initializecomponent();         }          public string pagetitle         {             { return title.text; }             set { title.text = value ?? ""; }         }     } } 

mainpage.xaml:

<page     x:class="app1.mainpage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="using:app1"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:ignorable="d">      <grid background="{themeresource applicationpagebackgroundthemebrush}">         <local:myheader pagetitle="page 1"/>     </grid> </page> 

Comments