title: Simple popup dialog in WPF (overlay inside Window)
body: <p>I'm working on a modal dialog popup <em>(I'm not sure about the exact UX term)</em> that is displayed inline, inside of a control or window with darkened background.</p>
  
  <p><strong>Visual example</strong></p>
  
  <p><a href="https://i.stack.imgur.com/MuJMM.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/MuJMM.jpg" alt="example"></a></p>
  
  <p>What I tried is putting a <code>&lt;ContentPresenter /&gt;</code> inside the XAML of the popup and then just instantiate it like this:</p>
  
  <pre><code>&lt;local:Popup Grid.RowSpan="2"&gt;
      &lt;TextBlock Text="Popup test..." /&gt;
  &lt;/local:Popup&gt;
  </code></pre>
  
  <p>However, the XAML replaces the entire Popup XAML instead of being placed where the ContentPresenter is.</p>
  
  <p><strong>Q:</strong> How is the ContentPresenter here used properly?</p>
  
  <p><strong>Popup.xaml</strong></p>
  
  <pre><code>&lt;ContentControl
      x:Class="[...].Popup"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="clr-namespace:[...]"
      mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="300"&gt;
      &lt;Grid Background="#7f000000"&gt;
          &lt;Grid Background="White" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;
              &lt;StackPanel Margin="20"&gt;
                  &lt;TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType=UserControl}}" FontSize="20" /&gt;
                  &lt;ContentPresenter /&gt;
              &lt;/StackPanel&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/ContentControl&gt;
  </code></pre>
  
  <p><strong>Popup.xaml.cs</strong></p>
  
  <pre><code>using System.Windows;
  
  namespace [...]
  {
      public partial class Popup : ContentControlBase
      {
          public static DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(Popup));
          public string Title
          {
              get
              {
                  return (string)GetValue(TitleProperty);
              }
              set
              {
                  SetValue(TitleProperty, value);
              }
          }
  
          public Popup()
          {
              InitializeComponent();
          }
      }
  }
  </code></pre>