title: Custom window class and adorner layer
body: <p>For design purposes I have implemented a custom window class with a style. The <em>StylableWindow</em> inherits from Window and mainly implements some dependency properties (e.g. for a title bar background color), does some border resizing logic and handles mouse interaction on the icon, titlebar and the additional menubay.</p>
  <p>When I was trying to get an AdornerLayer (with the AdornerLayer.GetAdornerLayer(uiElement) method) I always got <em>null</em> as result.</p>
  <p>After some investigation I tried to replace my custom window with the regular Window and voilà my adorners worked as intended.</p>
  <p>No my question is: <strong>What part have I done wrong in my custom window implementation so that WPF cannot find any AdornerLayer?</strong></p>
  <p>Here is my implementation of the style. As the StylableWindow.cs mostly consists of dependency properties and interaction logic I don't think that the code is very helpful for this purpose.</p>
  <pre><code>&lt;ResourceDictionary xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
                      xmlns:stylableWindow=&quot;clr-namespace:StylableWindow&quot;&gt;
  
      &lt;!--Base style for title bar buttons--&gt;
      &lt;Style x:Key=&quot;CaptionButtonStyle&quot; TargetType=&quot;Button&quot;&gt;
          &lt;Setter Property=&quot;Template&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                      &lt;Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;Transparent&quot; Width=&quot;44&quot; Height=&quot;30&quot;&gt;
                          &lt;TextBlock x:Name=&quot;txt&quot; Text=&quot;{TemplateBinding Content}&quot; FontFamily=&quot;Segoe MDL2 Assets&quot; FontSize=&quot;10&quot; 
                                     Foreground=&quot;{TemplateBinding Foreground}&quot; HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;
                                     RenderOptions.ClearTypeHint=&quot;Auto&quot; TextOptions.TextRenderingMode=&quot;Aliased&quot;  TextOptions.TextFormattingMode=&quot;Display&quot;/&gt;
                      &lt;/Grid&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
                              &lt;Setter TargetName=&quot;LayoutRoot&quot; Property=&quot;Background&quot; Value=&quot;#E5E5E5&quot;/&gt;
                              &lt;Setter TargetName=&quot;txt&quot; Property=&quot;Foreground&quot; Value=&quot;#000000&quot;/&gt;
                          &lt;/Trigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
      &lt;!--Minimize--&gt;
      &lt;Style x:Key=&quot;MinimizeButtonStyle&quot; TargetType=&quot;Button&quot; BasedOn=&quot;{StaticResource CaptionButtonStyle}&quot;&gt;
          &lt;Setter Property=&quot;Content&quot; Value=&quot;&amp;#xE949;&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;!--Maximize--&gt;
      &lt;Style x:Key=&quot;MaximizeButtonStyle&quot; TargetType=&quot;Button&quot; BasedOn=&quot;{StaticResource CaptionButtonStyle}&quot;&gt;
          &lt;Setter Property=&quot;Content&quot; Value=&quot;&amp;#xE739;&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;!--Restore--&gt;
      &lt;Style x:Key=&quot;RestoreButtonStyle&quot; TargetType=&quot;Button&quot; BasedOn=&quot;{StaticResource CaptionButtonStyle}&quot;&gt;
          &lt;Setter Property=&quot;Content&quot; Value=&quot;&amp;#xE923;&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;!--Close--&gt;
      &lt;Style x:Key=&quot;CloseButtonStyle&quot; TargetType=&quot;Button&quot; BasedOn=&quot;{StaticResource CaptionButtonStyle}&quot;&gt;
          &lt;Setter Property=&quot;Content&quot; Value=&quot;&amp;#xE106;&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType=&quot;stylableWindow:StylableWindow&quot; x:Key=&quot;StylableWindowStyle&quot;&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;{Binding Background}&quot;/&gt;
          &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Black&quot;/&gt;
          &lt;Setter Property=&quot;MinHeight&quot; Value=&quot;10&quot;/&gt;
          &lt;Setter Property=&quot;MinWidth&quot; Value=&quot;200&quot;/&gt;
          &lt;Setter Property=&quot;RenderOptions.BitmapScalingMode&quot; Value=&quot;HighQuality&quot;/&gt;
          &lt;Setter Property=&quot;Title&quot; Value=&quot;{Binding Title}&quot;/&gt;
          &lt;Setter Property=&quot;Icon&quot; Value=&quot;{Binding Icon}&quot;/&gt;
          &lt;Setter Property=&quot;WindowChrome.WindowChrome&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;WindowChrome GlassFrameThickness=&quot;1&quot; 
                                ResizeBorderThickness=&quot;4&quot;
                                CaptionHeight=&quot;0&quot;/&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
  
          &lt;Setter Property=&quot;Template&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType=&quot;{x:Type stylableWindow:StylableWindow}&quot;&gt;
  
                      &lt;Grid Background=&quot;Transparent&quot; x:Name=&quot;WindowRoot&quot;&gt;
  
                          &lt;Grid x:Name=&quot;LayoutRoot&quot;
                                Background=&quot;{TemplateBinding Background}&quot;&gt;
                              &lt;Grid.RowDefinitions&gt;
                                  &lt;RowDefinition Height=&quot;30&quot;/&gt;
                                  &lt;RowDefinition Height=&quot;*&quot;/&gt;
                              &lt;/Grid.RowDefinitions&gt;
  
                              &lt;!--TitleBar--&gt;
                              &lt;Grid x:Name=&quot;PART_HeaderBar&quot;
                                    Background=&quot;{TemplateBinding HeaderBarBackground}&quot;&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                      &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                      &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                      &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                                      &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
  
                                  &lt;ContentControl Grid.Column=&quot;0&quot;
                                                  x:Name=&quot;AppIcon&quot;
                                                  ContentTemplate=&quot;{TemplateBinding HeaderBarIcon}&quot;
                                                  Margin=&quot;6 0 10 0&quot;&gt;
                                  &lt;/ContentControl&gt;
  
                                  &lt;ContentControl Grid.Column=&quot;1&quot; 
                                                  Content=&quot;{TemplateBinding HeaderBarMenu}&quot;&gt;
                                  &lt;/ContentControl&gt;
  
                                  &lt;Rectangle Grid.Column=&quot;2&quot; Width=&quot;Auto&quot; Height=&quot;20&quot; Stroke=&quot;{TemplateBinding HeaderBarForeground}&quot; Visibility=&quot;{TemplateBinding TitleBorderVisibility}&quot; Margin=&quot;0 2 0 0&quot;/&gt;
                                  &lt;TextBlock Text=&quot;{TemplateBinding Title}&quot; 
                                             Grid.Column=&quot;2&quot;
                                             TextTrimming=&quot;CharacterEllipsis&quot;
                                             FontSize=&quot;14&quot;
                                             TextAlignment=&quot;Center&quot;
                                             VerticalAlignment=&quot;Center&quot;
                                             Padding=&quot;10 0 10 0&quot;
                                             Foreground=&quot;{TemplateBinding HeaderBarForeground}&quot;
                                             Panel.ZIndex=&quot;0&quot;
                                             IsEnabled=&quot;{TemplateBinding IsActive}&quot;/&gt;
  
                                  &lt;Grid x:Name=&quot;WindowControlsGrid&quot; Grid.Column=&quot;4&quot; Background=&quot;Transparent&quot;&gt;
                                      &lt;Grid.ColumnDefinitions&gt;
                                          &lt;ColumnDefinition/&gt;
                                          &lt;ColumnDefinition/&gt;
                                          &lt;ColumnDefinition/&gt;
                                      &lt;/Grid.ColumnDefinitions&gt;
  
                                      &lt;Button x:Name=&quot;MinimizeButton&quot;
                                              Foreground=&quot;{TemplateBinding HeaderBarForeground}&quot;
                                              Style=&quot;{StaticResource MinimizeButtonStyle}&quot; 
                                              Visibility=&quot;{TemplateBinding MinMaxVisibility}&quot;
                                              Grid.Column=&quot;0&quot;/&gt;
                                      &lt;Button x:Name=&quot;MaximizeButton&quot; 
                                              Foreground=&quot;{TemplateBinding HeaderBarForeground}&quot;
                                              Style=&quot;{StaticResource MaximizeButtonStyle}&quot; 
                                              Visibility=&quot;{TemplateBinding MinMaxVisibility}&quot;
                                              Grid.Column=&quot;1&quot;/&gt;
                                      &lt;Button x:Name=&quot;RestoreButton&quot; 
                                              Foreground=&quot;{TemplateBinding HeaderBarForeground}&quot;
                                              Style=&quot;{StaticResource RestoreButtonStyle}&quot; 
                                              Visibility=&quot;Collapsed&quot;
                                              Grid.Column=&quot;1&quot;/&gt;
                                      &lt;Button x:Name=&quot;CloseButton&quot; 
                                              Foreground=&quot;{TemplateBinding HeaderBarForeground}&quot;
                                              Style=&quot;{StaticResource CloseButtonStyle}&quot; 
                                              Grid.Column=&quot;2&quot;/&gt;
                                  &lt;/Grid&gt;
                              &lt;/Grid&gt;
  
                              &lt;Grid x:Name=&quot;PART_MainContentGrid&quot;
                                    Grid.Row=&quot;1&quot;
                                    Panel.ZIndex=&quot;10&quot;&gt;
                                  &lt;ContentPresenter x:Name=&quot;PART_MainContentPresenter&quot; Grid.Row=&quot;0&quot;/&gt;
                              &lt;/Grid&gt;
                          &lt;/Grid&gt;
                      &lt;/Grid&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
      &lt;!--&lt;Style TargetType=&quot;{x:Type Window}&quot; BasedOn=&quot;{StaticResource StylableWindowStyle}&quot;/&gt;--&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>
  <pre><code>&lt;StylableWindow Style=&quot;{StaticResource StylableWindowStyle}&quot;&gt;
      &lt;StylableWindow.HeaderBarMenu&gt;
          &lt;!-- a menu bar for the title --&gt;
      &lt;/StylableWindow.HeaderBarMenu&gt;
  
      &lt;Grid&gt;
     &lt;!-- content goes here --&gt;
      &lt;/Grid&gt;
  &lt;/StylableWindow&gt;
  </code></pre>