title: ListViewItem Style works from App.xaml while doesn't work from User Control xaml
body: <p>I wrote my own user control that contains a ListView and its ItemsSource is set runtime.
  I'd want to set my own ListViewItem style from the user control xaml but I've not been able to get it working, anyway, setting it from App.xaml works flawlessy so I wonder why?</p>
  <p>The user control xaml is the following one:</p>
  <pre><code>&lt;UserControl x:Class=&quot;LogManagerWPF.LogManager&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
               xmlns:local=&quot;clr-namespace:LogManagerWPF&quot;
               mc:Ignorable=&quot;d&quot; 
               d:DesignHeight=&quot;192&quot; d:DesignWidth=&quot;400&quot;
               DataContext=&quot;{Binding RelativeSource={RelativeSource Self}}&quot;&gt;
      &lt;UserControl.Resources&gt;
          &lt;DataTemplate x:Key=&quot;DisplayImage&quot;&gt;
              &lt;Image Source=&quot;{Binding ImageIcon}&quot; Width=&quot;16&quot; Height=&quot;16&quot;/&gt;
          &lt;/DataTemplate&gt;
          &lt;Style TargetType=&quot;{x:Type ListViewItem}&quot;&gt;
              &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Stretch&quot; /&gt;
              &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot; /&gt;
          &lt;/Style&gt;
      &lt;/UserControl.Resources&gt;
      &lt;ListView Margin=&quot;0&quot; x:Name=&quot;m_lvLog&quot; ScrollViewer.VerticalScrollBarVisibility=&quot;Auto&quot; ScrollViewer.HorizontalScrollBarVisibility=&quot;Auto&quot; ContextMenuOpening=&quot;m_lvLog_ContextMenuOpening&quot; SelectionChanged=&quot;m_lvLog_SelectionChanged&quot;&gt;
          &lt;ListView.Resources&gt;
              &lt;!-- Windows 7 --&gt;
              &lt;SolidColorBrush x:Key=&quot;{x:Static SystemColors.HighlightBrushKey}&quot; Color=&quot;{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}&quot; /&gt;
              &lt;SolidColorBrush x:Key=&quot;{x:Static SystemColors.ActiveBorderBrushKey}&quot; Color=&quot;{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}&quot; /&gt;
              &lt;!-- Windows 10 --&gt;
              &lt;Style TargetType=&quot;{x:Type ListViewItem}&quot;&gt;
                  &lt;Style.Triggers&gt;
                      &lt;Trigger Property=&quot;IsSelected&quot; Value=&quot;True&quot;&gt;
                          &lt;Setter Property=&quot;Background&quot;&gt;
                              &lt;Setter.Value&gt;
                                  &lt;SolidColorBrush Color=&quot;{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}&quot;&gt;&lt;/SolidColorBrush&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                          &lt;Setter Property=&quot;BorderBrush&quot;&gt;
                              &lt;Setter.Value&gt;
                                  &lt;SolidColorBrush Color=&quot;{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}&quot;&gt;&lt;/SolidColorBrush&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                      &lt;/Trigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/ListView.Resources&gt;
          &lt;ListView.View&gt;
              &lt;GridView x:Name=&quot;m_gvLog&quot; AllowsColumnReorder=&quot;False&quot;&gt;
                  &lt;GridViewColumn x:Name=&quot;m_icon&quot; Width=&quot;28&quot; Header=&quot;&quot; CellTemplate=&quot;{StaticResource DisplayImage}&quot;/&gt;
                  &lt;GridViewColumn x:Name=&quot;m_time&quot; Width=&quot;Auto&quot; Header=&quot;Timestamp&quot; DisplayMemberBinding=&quot;{Binding Timestamp}&quot;&gt;
                      &lt;GridViewColumn.HeaderContainerStyle&gt;
                          &lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot;&gt;
                              &lt;Setter Property=&quot;Foreground&quot; Value=&quot;#FF042271&quot; /&gt;
                              &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
                              &lt;Setter Property=&quot;Padding&quot; Value=&quot;10,0,0,0&quot; /&gt;
                          &lt;/Style&gt;
                      &lt;/GridViewColumn.HeaderContainerStyle&gt;
                  &lt;/GridViewColumn&gt;
                  &lt;GridViewColumn x:Name=&quot;m_messages&quot; Width=&quot;100&quot; Header=&quot;Message&quot; DisplayMemberBinding=&quot;{Binding Message}&quot;&gt;
                      &lt;GridViewColumn.HeaderContainerStyle&gt;
                          &lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot;&gt;
                              &lt;Setter Property=&quot;Foreground&quot; Value=&quot;#FF042271&quot; /&gt;
                              &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Left&quot; /&gt;
                              &lt;Setter Property=&quot;Padding&quot; Value=&quot;10,0,0,0&quot; /&gt;
                          &lt;/Style&gt;
                      &lt;/GridViewColumn.HeaderContainerStyle&gt;
                  &lt;/GridViewColumn&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
          &lt;ListView.ContextMenu&gt;
              &lt;ContextMenu x:Name=&quot;m_contextMenu&quot;&gt;
                  &lt;MenuItem x:Name=&quot;m_menuAutoScroll&quot; Header=&quot;Autoscroll&quot; Click=&quot;m_menuAutoScroll_Click&quot; IsCheckable=&quot;True&quot; /&gt;
                  &lt;MenuItem x:Name=&quot;m_menuClear&quot; Header=&quot;Clear Log&quot; Click=&quot;m_menuClear_Click&quot;/&gt;
                  &lt;MenuItem x:Name=&quot;m_menuCopy&quot; Header=&quot;Copy to Clipboard&quot; Click=&quot;m_menuCopy_Click&quot;/&gt;
                  &lt;MenuItem x:Name=&quot;m_menuOpenLog&quot; Header=&quot;Open Log&quot; Click=&quot;m_menuOpenLog_Click&quot;/&gt;
                  &lt;MenuItem x:Name=&quot;m_menuHorizontalScrollBarVisibility&quot; Header=&quot;Set Horizontal ScrollBar Visibility&quot;&gt;
                      &lt;MenuItem x:Name=&quot;m_menuHorizontalScrollBarEnabled&quot; Header=&quot;Enabled&quot; Click=&quot;m_menuHorizontalScrollBarEnabled_Click&quot; IsCheckable=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuHorizontalScrollBarDisabled&quot; Header=&quot;Disabled&quot; Click=&quot;m_menuHorizontalScrollBarDisabled_Click&quot; IsCheckable=&quot;True&quot;/&gt;
                  &lt;/MenuItem&gt;
                  &lt;MenuItem x:Name=&quot;m_menuVerbosity&quot; Header=&quot;Set Verbosity&quot;&gt;
                      &lt;MenuItem x:Name=&quot;m_menuVerbosityLow&quot; Header=&quot;Low&quot; Click=&quot;m_menuVerbosityLow_Click&quot; IsCheckable=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuVerbosityStandard&quot; Header=&quot;Standard&quot; Click=&quot;m_menuVerbosityStandard_Click&quot; IsCheckable=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuVerbosityHigh&quot; Header=&quot;High&quot; Click=&quot;m_menuVerbosityHigh_Click&quot; IsCheckable=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuVerbosityDebug&quot; Header=&quot;Debug&quot; Click=&quot;m_menuVerbosityDebug_Click&quot; IsCheckable=&quot;True&quot;/&gt;
                  &lt;/MenuItem&gt;
                  &lt;MenuItem x:Name=&quot;m_menuSeverityFilter&quot; Header=&quot;Set Severity Filter&quot;&gt;
                      &lt;MenuItem x:Name=&quot;m_menuSeverityFilterDebug&quot; Header=&quot;Debug&quot; Checked=&quot;m_menuSeverityFilterDebug_CheckedChanged&quot; Unchecked=&quot;m_menuSeverityFilterDebug_CheckedChanged&quot; IsCheckable=&quot;True&quot; StaysOpenOnClick=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuSeverityFilterInformation&quot; Header=&quot;Information&quot; Checked=&quot;m_menuSeverityFilterInformation_CheckedChanged&quot; Unchecked=&quot;m_menuSeverityFilterInformation_CheckedChanged&quot; IsCheckable=&quot;True&quot; StaysOpenOnClick=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuSeverityFilterWarning&quot; Header=&quot;Warning&quot; Checked=&quot;m_menuSeverityFilterWarning_CheckedChanged&quot; Unchecked=&quot;m_menuSeverityFilterWarning_CheckedChanged&quot; IsCheckable=&quot;True&quot; StaysOpenOnClick=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuSeverityFilterError&quot; Header=&quot;Error&quot; Checked=&quot;m_menuSeverityFilterError_CheckedChanged&quot; Unchecked=&quot;m_menuSeverityFilterError_CheckedChanged&quot; IsCheckable=&quot;True&quot; StaysOpenOnClick=&quot;True&quot;/&gt;
                      &lt;Separator/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuSeverityFilterAll&quot; Header=&quot;All&quot; Click=&quot;m_menuSeverityFilterAll_Click&quot; StaysOpenOnClick=&quot;True&quot;/&gt;
                      &lt;MenuItem x:Name=&quot;m_menuSeverityFilterNone&quot; Header=&quot;None&quot; Click=&quot;m_menuSeverityFilterNone_Click&quot; StaysOpenOnClick=&quot;True&quot;/&gt;
                  &lt;/MenuItem&gt;
                  &lt;MenuItem x:Name=&quot;m_menuContentFilter&quot; Header=&quot;Set Content Filter&quot;&gt;
                      &lt;MenuItem x:Name=&quot;m_menuContentFilterSet&quot;&gt;
                          &lt;MenuItem.Header&gt;
                              &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;
                                  &lt;Label Content=&quot;Set&quot; Padding=&quot;0&quot;/&gt;
                                  &lt;TextBox x:Name=&quot;m_menuContentFilterString&quot; Width=&quot;150&quot; Padding=&quot;0&quot; Margin=&quot;10,0,0,0&quot; VerticalContentAlignment=&quot;Center&quot; LostFocus=&quot;m_menuContentFilterString_LostFocus&quot;/&gt;
                              &lt;/StackPanel&gt;
                          &lt;/MenuItem.Header&gt;
                      &lt;/MenuItem&gt;
                      &lt;MenuItem x:Name=&quot;m_menuContentFilterClear&quot; Header=&quot;Clear&quot; Click=&quot;m_menuContentFilterClear_Click&quot;/&gt;
                  &lt;/MenuItem&gt;
              &lt;/ContextMenu&gt;
          &lt;/ListView.ContextMenu&gt;
      &lt;/ListView&gt;
  &lt;/UserControl&gt;
  </code></pre>
  <p>The styleI'd want to apply works from App.xaml this way:</p>
  <pre><code>&lt;Application x:Class=&quot;LogManagerWpf_Tester.App&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               StartupUri=&quot;MainWindow.xaml&quot;&gt;
      &lt;Application.Resources&gt;
          &lt;Style TargetType=&quot;{x:Type ListViewItem}&quot;&gt;
              &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Stretch&quot; /&gt;
              &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot; /&gt;
          &lt;/Style&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  <p>I know the style is not being applied when done from the user control itself because in the output window I see the following errors for each ListViewItem added, that instead doesn't shows when applying the style from the App.xaml:</p>
  <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')</p>
  <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')</p>