Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I style a control with the Aero theme applied in WPF 4.0?
    primarykey
    data
    text
    <p>I recently converted a project from WPF 3.5 to WPF 4.0. Functionally, everything works, but the DataGrid style I was applying on top of the Aero theme has suddenly stopped working. As you can see from the before/after pictures below, my DataGrids went from having an Aero look plus bold headings, extra padding, and alternating row formats to just looking plain "Aero". Besides removing all references to the WPF Toolkit (since the DataGrid is now native to WPF 4.0), I really didn't change anything about my code/markup.</p> <p><strong>Before (WPF Toolkit DataGrid)</strong></p> <p><img src="https://i.stack.imgur.com/ncYN5.png" alt="Looks like Aero w/ bold headings, extra padding, and alternate row styles"></p> <p><strong>After (.NET 4.0 DataGrid)</strong></p> <p><img src="https://i.stack.imgur.com/JUohX.png" alt="Looks like Aero w/ nothing"></p> <p>As I learned in <a href="https://stackoverflow.com/questions/4235926/why-did-my-datagrid-styling-break-when-upgrading-from-net-3-5-w-wpf-toolkit-to">an earlier question</a>, I am able to get the custom DataGrid styling to work again if I stop referencing the Aero resource dictionary, but then everything looks "Luna" on Windows XP (which is not what I want).</p> <p>So, how do I ensure that my app always uses the Aero theme, but still apply styling on top of that theme <em>in WPF 4.0</em>?</p> <p><strong>Here is my App.xaml code:</strong></p> <pre><code>&lt;Application x:Class="TempProj.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" /&gt; &lt;ResourceDictionary Source="/CommonLibraryWpf;component/ResourceDictionaries/DataGridResourceDictionary.xaml" /&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; &lt;/Application&gt; </code></pre> <p><strong>Here is my DataGridResourceDictionary.xaml code:</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Style x:Key="DataGrid_ColumnHeaderStyle" TargetType="DataGridColumnHeader"&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;Setter Property="TextBlock.TextAlignment" Value="Center" /&gt; &lt;Setter Property="TextBlock.TextWrapping" Value="WrapWithOverflow" /&gt; &lt;/Style&gt; &lt;Style x:Key="DataGrid_CellStyle" TargetType="DataGridCell"&gt; &lt;Setter Property="Padding" Value="5,5,5,5" /&gt; &lt;Setter Property="TextBlock.TextAlignment" Value="Center" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="DataGridCell"&gt; &lt;Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"&gt; &lt;ContentPresenter /&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style TargetType="DataGrid"&gt; &lt;Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGrid_ColumnHeaderStyle}" /&gt; &lt;Setter Property="CellStyle" Value="{StaticResource DataGrid_CellStyle}" /&gt; &lt;Setter Property="Background" Value="White" /&gt; &lt;Setter Property="AlternatingRowBackground" Value="#F0F0F0" /&gt; &lt;Setter Property="VerticalGridLinesBrush" Value="LightGray" /&gt; &lt;Setter Property="HeadersVisibility" Value="Column" /&gt; &lt;Setter Property="SelectionMode" Value="Single" /&gt; &lt;Setter Property="SelectionUnit" Value="FullRow" /&gt; &lt;Setter Property="GridLinesVisibility" Value="Vertical" /&gt; &lt;Setter Property="AutoGenerateColumns" Value="False" /&gt; &lt;Setter Property="CanUserAddRows" Value="False" /&gt; &lt;Setter Property="CanUserDeleteRows" Value="False" /&gt; &lt;Setter Property="CanUserReorderColumns" Value="True" /&gt; &lt;Setter Property="CanUserResizeColumns" Value="True" /&gt; &lt;Setter Property="CanUserResizeRows" Value="False" /&gt; &lt;Setter Property="CanUserSortColumns" Value="True" /&gt; &lt;Setter Property="IsReadOnly" Value="True" /&gt; &lt;Setter Property="BorderBrush" Value="#DDDDDD" /&gt; &lt;Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" /&gt; &lt;Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" /&gt; &lt;/Style&gt; &lt;Style x:Key="DataGrid_FixedStyle" TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}"&gt; &lt;Setter Property="CanUserReorderColumns" Value="False" /&gt; &lt;Setter Property="CanUserResizeColumns" Value="False" /&gt; &lt;Setter Property="CanUserResizeRows" Value="False" /&gt; &lt;Setter Property="CanUserSortColumns" Value="False" /&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p><strong>Here's a usage sample:</strong></p> <pre><code>&lt;DataGrid Grid.Row="0" Grid.Column="0" Style="{StaticResource DataGrid_FixedStyle}" ItemsSource="{Binding Coordinates}"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding X}" Header="X" /&gt; &lt;DataGridTextColumn Binding="{Binding Y}" Header="Y" /&gt; &lt;DataGridTextColumn Binding="{Binding Z}" Header="Z" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p><strong>Edit</strong></p> <p>It just occurred to me that maybe the problem is that I'm referencing the wrong version of the Aero framework.</p> <p>Here's what I have now:</p> <pre><code>&lt;ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" /&gt; </code></pre> <p>Should this be updated to version 4.0? What is the <code>PublicKeyToken</code> for version 4 (or how do I figure this out)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload