title: What is the best way to reuse blocks of XAML?
body: <p>I've got many user controls like this:</p>
  
  <p><strong>PageManageCustomers.xaml.cs:</strong></p>
  
  <pre><code>public partial class PageManageCustomers : BasePage
  {
   ...
  }
  </code></pre>
  
  <p>which inherit from:</p>
  
  <p><strong>PageBase.cs:</strong></p>
  
  <pre><code>public class BasePage : UserControl, INotifyPropertyChanged
  {
   ...
  }
  </code></pre>
  
  <p>Since <strong>PageBase.cs</strong> has <strong>no accompanying XAML file</strong>, I have to put the XAML that it refers to in <strong>each</strong> of the user controls which inherit it, e.g. the following block is repeated in <strong>every</strong> XAML file of every control that inherits <code>PageBase</code>:</p>
  
  <pre><code>&lt;DataTemplate x:Key="manageAreaCellTemplate"&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;TextBlock Style="{DynamicResource ManageLinkStyle}"
      Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/&gt;
          &lt;TextBlock Text=" "/&gt;
          &lt;TextBlock Style="{DynamicResource ManageLinkStyle}"
             Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/&gt;
      &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>I'm trying to put this block into a <strong>resource file</strong> but can't get the syntax right, it says:</p>
  
  <blockquote>
    <p>'ResourceDictionary' root element
    requires a x:Class attribute to
    support event handlers in the XAML
    file. Either remove the event handler
    for the MouseDown event, or add a
    x:Class attribute to the root element.</p>
  </blockquote>
  
  <p>Or perhaps I could read these blocks in with <strong>XamlReader</strong>` somehow?</p>
  
  <p><strong>How can I put this repeated block of code in one place so that it is not repeated in every XAML file that inherits BagePage?</strong></p>
  
  <h1>Here is a reproducable example of this problem:</h1>
  
  <p><strong>Window1.xaml:</strong></p>
  
  <pre><code>&lt;Window x:Class="TestXamlPage8283.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;StackPanel x:Name="MainContent"/&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Window1.xaml.cs:</strong></p>
  
  <pre><code>using System.Windows;
  
  namespace TestXamlPage8283
  {
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
  
              Page1 page1 = new Page1();
              MainContent.Children.Add(page1);
  
              Page2 page2 = new Page2();
              MainContent.Children.Add(page2);
          }
      }
  }
  </code></pre>
  
  <p><strong>Page1.xaml:</strong></p>
  
  <pre><code>&lt;local:BasePage x:Class="TestXamlPage8283.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:TestXamlPage8283"
      Height="40" Width="300"&gt;
      &lt;StackPanel&gt;
          &lt;TextBlock Text="{Binding PageTitle}"
                     FontSize="14"
                     FontWeight="Bold"/&gt;
          &lt;TextBlock Text="This is XAML that is specific to page one." /&gt;
      &lt;/StackPanel&gt;
  &lt;/local:BasePage&gt;
  </code></pre>
  
  <p><strong>Page1.xaml.cs:</strong></p>
  
  <pre><code>namespace TestXamlPage8283
  {
      public partial class Page1 : BasePage
      {
          public Page1()
          {
              InitializeComponent();
              PageTitle = "Page One";
          }
      }
  }
  </code></pre>
  
  <p><strong>Page2.xaml:</strong></p>
  
  <pre><code>&lt;local:BasePage x:Class="TestXamlPage8283.Page2"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:TestXamlPage8283"
      Height="40" Width="300"&gt;
      &lt;StackPanel&gt;
          &lt;TextBlock Text="{Binding PageTitle}"
                     FontSize="14"
                     FontWeight="Bold"/&gt;
          &lt;TextBlock Text="This is XAML that is specific to page two." /&gt;
      &lt;/StackPanel&gt;
  &lt;/local:BasePage&gt;
  </code></pre>
  
  <p><strong>Page2.xaml.cs:</strong></p>
  
  <pre><code>namespace TestXamlPage8283
  {
      public partial class Page2 : BasePage
      {
          public Page2()
          {
              InitializeComponent();
              PageTitle = "Page Two";
          }
      }
  }
  </code></pre>
  
  <p><strong>BasePage.cs:</strong></p>
  
  <pre><code>using System.Windows.Controls;
  using System.ComponentModel;
  
  namespace TestXamlPage8283
  {
      public class BasePage : UserControl, INotifyPropertyChanged
      {
          #region ViewModelProperty: PageTitle
          private string _pageTitle;
          public string PageTitle
          {
              get
              {
                  return _pageTitle;
              }
  
              set
              {
                  _pageTitle = value;
                  OnPropertyChanged("PageTitle");
              }
          }
          #endregion
  
          public BasePage()
          {
              DataContext = this;
          }
  
          #region INotifiedProperty Block
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected void OnPropertyChanged(string propertyName)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
  
              if (handler != null)
              {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              }
          }
          #endregion
      }
  }
  </code></pre>
  
  <p><strong>How do I take <em>this</em> block</strong></p>
  
  <pre><code>&lt;TextBlock Text="{Binding PageTitle}"
             FontSize="14"
             FontWeight="Bold"/&gt;
  </code></pre>
  
  <p><strong><em>out</em> of Page1.xaml and Page2.xaml and put it in <em>one</em> place so I can <em>refer</em> to it from Page1.xaml and Page2.xaml?</strong> (so that when I want to change FontSize=14 to FontSize=16, I just have to change it in one place)</p>