title: how to set the style of a nested control?
body: <p>I have a ScrollBar style in my project that is automatically used by all ScrollBar instances, including controls that have a ScrollBar in them.  The time has come to use a DataGrid in the project, but I do not want it to take on the ScrollBar style because it clashes with the rest of the datagrid's default style, and I don't have time to figure out how to make a DataGrid style that will agree with the scrollbar. </p>
  
  <p>After googling this issue all morning, I tried this solution that I found:  I used my trial version of expression blend to save the default style of the ScrollBar as a resource dictionary to a separate xaml file.  I added this xaml file to my project and merged it with the rest of the resource dictionaries at the application level.  In the Window xaml file where the DataGrid is used, I tried this:</p>
  
  <pre><code>&lt;Window x:Class="Chart.ChartDataGrid.ChartDataGridWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:Chart.ChartDataGrid"
      Title="ChartDataGridWindow" Height="300" Width="300"
      WindowStartupLocation="CenterOwner"
      &gt;
  &lt;Grid&gt;
      &lt;DataGrid ItemsSource="{Binding ValuesView}" AutoGenerateColumns="False"
                x:Name="_dataGrid"&gt;
          &lt;DataGrid.Resources&gt;
              &lt;Style TargetType="ScrollBar" BasedOn="{StaticResource {x:Static ScrollBarStyleDefault}}" /&gt;
          &lt;/DataGrid.Resources&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="Date/Time" Binding="{Binding TradeDate}" /&gt;
              &lt;DataGridTextColumn Header="Price" Binding="{Binding Price}" /&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>When compiling, the style in  will produce an error:</p>
  
  <p>error MC3029: 'ScrollBarStyleDefault' member is not valid because it does not have a qualifying type name.</p>
  
  <p>Here is the Scrollbar style xaml file, which I got from Expression Blend:</p>
  
  <pre><code>&lt;ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"&gt;
  
  &lt;LinearGradientBrush x:Key="VerticalScrollBarBackgroundDefault" EndPoint="1,0" StartPoint="0,0"&gt;
      &lt;GradientStop Color="#E1E1E1" Offset="0"/&gt;
      &lt;GradientStop Color="#EDEDED" Offset="0.20"/&gt;
      &lt;GradientStop Color="#EDEDED" Offset="0.80"/&gt;
      &lt;GradientStop Color="#E3E3E3" Offset="1"/&gt;
  &lt;/LinearGradientBrush&gt;
  &lt;!-- Resource dictionary entries should be defined here. --&gt;
  &lt;SolidColorBrush x:Key="ScrollBarDisabledBackgroundDefault" Color="#F4F4F4"/&gt;
  &lt;Style x:Key="ScrollBarButtonDefault" TargetType="{x:Type RepeatButton}"&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
      &lt;Setter Property="Focusable" Value="false"/&gt;
      &lt;Setter Property="IsTabStop" Value="false"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type RepeatButton}"&gt;
                  &lt;Microsoft_Windows_Themes:ScrollChrome x:Name="Chrome" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="{TemplateBinding Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph}"/&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;Style x:Key="VerticalScrollBarPageButtonDefault" TargetType="{x:Type RepeatButton}"&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
      &lt;Setter Property="Background" Value="Transparent"/&gt;
      &lt;Setter Property="Focusable" Value="false"/&gt;
      &lt;Setter Property="IsTabStop" Value="false"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type RepeatButton}"&gt;
                  &lt;Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;Style x:Key="ScrollBarThumbDefault" TargetType="{x:Type Thumb}"&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
      &lt;Setter Property="IsTabStop" Value="false"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Thumb}"&gt;
                  &lt;Microsoft_Windows_Themes:ScrollChrome x:Name="Chrome" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsDragging}" SnapsToDevicePixels="true" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="{TemplateBinding Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph}"/&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;LinearGradientBrush x:Key="HorizontalScrollBarBackgroundDefault" EndPoint="0,1" StartPoint="0,0"&gt;
      &lt;GradientStop Color="#E1E1E1" Offset="0"/&gt;
      &lt;GradientStop Color="#EDEDED" Offset="0.20"/&gt;
      &lt;GradientStop Color="#EDEDED" Offset="0.80"/&gt;
      &lt;GradientStop Color="#E3E3E3" Offset="1"/&gt;
  &lt;/LinearGradientBrush&gt;
  &lt;Style x:Key="HorizontalScrollBarPageButtonDefault" TargetType="{x:Type RepeatButton}"&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
      &lt;Setter Property="Background" Value="Transparent"/&gt;
      &lt;Setter Property="Focusable" Value="false"/&gt;
      &lt;Setter Property="IsTabStop" Value="false"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type RepeatButton}"&gt;
                  &lt;Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;Style x:Key="ScrollBarDefaultStyle" TargetType="{x:Type ScrollBar}"&gt;
      &lt;Setter Property="Background" Value="{StaticResource VerticalScrollBarBackgroundDefault}"/&gt;
      &lt;Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/&gt;
      &lt;Setter Property="Stylus.IsFlicksEnabled" Value="false"/&gt;
      &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
      &lt;Setter Property="Width" Value="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/&gt;
      &lt;Setter Property="MinWidth" Value="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ScrollBar}"&gt;
                  &lt;Grid x:Name="Bg" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/&gt;
                          &lt;RowDefinition Height="0.00001*"/&gt;
                          &lt;RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;RepeatButton Command="{x:Static ScrollBar.LineUpCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource ScrollBarButtonDefault}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="UpArrow"/&gt;
                      &lt;Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="1"&gt;
                          &lt;Track.DecreaseRepeatButton&gt;
                              &lt;RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource VerticalScrollBarPageButtonDefault}"/&gt;
                          &lt;/Track.DecreaseRepeatButton&gt;
                          &lt;Track.IncreaseRepeatButton&gt;
                              &lt;RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource VerticalScrollBarPageButtonDefault}"/&gt;
                          &lt;/Track.IncreaseRepeatButton&gt;
                          &lt;Track.Thumb&gt;
                              &lt;Thumb Style="{StaticResource ScrollBarThumbDefault}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="VerticalGripper"/&gt;
                          &lt;/Track.Thumb&gt;
                      &lt;/Track&gt;
                      &lt;RepeatButton Command="{x:Static ScrollBar.LineDownCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="2" Style="{StaticResource ScrollBarButtonDefault}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="DownArrow"/&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsEnabled" Value="false"&gt;
                          &lt;Setter Property="Background" TargetName="Bg" Value="{StaticResource ScrollBarDisabledBackgroundDefault}"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
      &lt;Style.Triggers&gt;
          &lt;Trigger Property="Orientation" Value="Horizontal"&gt;
              &lt;Setter Property="Width" Value="Auto"/&gt;
              &lt;Setter Property="MinWidth" Value="0"/&gt;
              &lt;Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarHeightKey}}"/&gt;
              &lt;Setter Property="MinHeight" Value="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarHeightKey}}"/&gt;
              &lt;Setter Property="Background" Value="{StaticResource HorizontalScrollBarBackgroundDefault}"/&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type ScrollBar}"&gt;
                          &lt;Grid x:Name="Bg" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}"/&gt;
                                  &lt;ColumnDefinition Width="0.00001*"/&gt;
                                  &lt;ColumnDefinition MaxWidth="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarButtonWidthKey}}"/&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;RepeatButton Command="{x:Static ScrollBar.LineLeftCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource ScrollBarButtonDefault}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="LeftArrow"/&gt;
                              &lt;Track x:Name="PART_Track" Grid.Column="1" IsEnabled="{TemplateBinding IsMouseOver}"&gt;
                                  &lt;Track.DecreaseRepeatButton&gt;
                                      &lt;RepeatButton Command="{x:Static ScrollBar.PageLeftCommand}" Style="{StaticResource HorizontalScrollBarPageButtonDefault}"/&gt;
                                  &lt;/Track.DecreaseRepeatButton&gt;
                                  &lt;Track.IncreaseRepeatButton&gt;
                                      &lt;RepeatButton Command="{x:Static ScrollBar.PageRightCommand}" Style="{StaticResource HorizontalScrollBarPageButtonDefault}"/&gt;
                                  &lt;/Track.IncreaseRepeatButton&gt;
                                  &lt;Track.Thumb&gt;
                                      &lt;Thumb Style="{StaticResource ScrollBarThumbDefault}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="HorizontalGripper"/&gt;
                                  &lt;/Track.Thumb&gt;
                              &lt;/Track&gt;
                              &lt;RepeatButton Grid.Column="2" Command="{x:Static ScrollBar.LineRightCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource ScrollBarButtonDefault}" Microsoft_Windows_Themes:ScrollChrome.ScrollGlyph="RightArrow"/&gt;
                          &lt;/Grid&gt;
                          &lt;ControlTemplate.Triggers&gt;
                              &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                  &lt;Setter Property="Background" TargetName="Bg" Value="{StaticResource ScrollBarDisabledBackgroundDefault}"/&gt;
                              &lt;/Trigger&gt;
                          &lt;/ControlTemplate.Triggers&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>And here is my App.xaml file:</p>
  
  <pre><code>&lt;Application x:Class="StockChart.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  StartupUri="MainWindow.xaml"&gt;
  &lt;Application.Resources&gt;
      &lt;ResourceDictionary&gt;
            &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="Themes/BlackGloss.xaml"    /&gt;
              &lt;ResourceDictionary Source="Themes/ScrollBarDefault.xaml" /&gt;
            &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>Does anyone know how to do this?</p>
  
  <p>Thanks!</p>
  
  <p>Alex</p>