Недавно я преобразовал проект из WPF 3.5 в WPF 4.0. Функционально все работает, но стиль DataGrid, который я применял поверх темы Aero, внезапно прекратил работать. Как вы можете видеть из рисунков до/после ниже, мои DataGrids перешли от того, что Aero выглядят плюс смелые заголовки, дополнительные дополнения и чередующиеся форматы строк только для простого "Aero". Помимо удаления всех ссылок на WPF Toolkit (поскольку DataGrid теперь является родным для WPF 4.0), я действительно ничего не менял о своем коде/разметке.
До (WPF Toolkit DataGrid)
После (.NET 4.0 DataGrid)
Как я узнал в более раннем вопросе, я могу заставить пользовательский стиль DataGrid снова работать, если перестать ссылаться на словарь ресурсов Aero, но потом все выглядит "Луна" в Windows XP (это не то, что я хочу).
Итак, как я могу гарантировать, что мое приложение всегда использует тему Aero, но все еще применяет стиль поверх этой темы в WPF 4.0?
Вот мой код App.xaml:
<Application
x:Class="TempProj.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero,
Version=3.0.0.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
<ResourceDictionary Source="/CommonLibraryWpf;component/ResourceDictionaries/DataGridResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Вот мой код DataGridResourceDictionary.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="DataGrid_ColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="TextBlock.TextWrapping" Value="WrapWithOverflow" />
</Style>
<Style x:Key="DataGrid_CellStyle" TargetType="DataGridCell">
<Setter Property="Padding" Value="5,5,5,5" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGrid_ColumnHeaderStyle}" />
<Setter Property="CellStyle" Value="{StaticResource DataGrid_CellStyle}" />
<Setter Property="Background" Value="White" />
<Setter Property="AlternatingRowBackground" Value="#F0F0F0" />
<Setter Property="VerticalGridLinesBrush" Value="LightGray" />
<Setter Property="HeadersVisibility" Value="Column" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="SelectionUnit" Value="FullRow" />
<Setter Property="GridLinesVisibility" Value="Vertical" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="CanUserAddRows" Value="False" />
<Setter Property="CanUserDeleteRows" Value="False" />
<Setter Property="CanUserReorderColumns" Value="True" />
<Setter Property="CanUserResizeColumns" Value="True" />
<Setter Property="CanUserResizeRows" Value="False" />
<Setter Property="CanUserSortColumns" Value="True" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="BorderBrush" Value="#DDDDDD" />
<Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" />
<Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" />
</Style>
<Style x:Key="DataGrid_FixedStyle" TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="CanUserReorderColumns" Value="False" />
<Setter Property="CanUserResizeColumns" Value="False" />
<Setter Property="CanUserResizeRows" Value="False" />
<Setter Property="CanUserSortColumns" Value="False" />
</Style>
</ResourceDictionary>
Здесь пример использования:
<DataGrid
Grid.Row="0"
Grid.Column="0"
Style="{StaticResource DataGrid_FixedStyle}"
ItemsSource="{Binding Coordinates}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding X}" Header="X" />
<DataGridTextColumn Binding="{Binding Y}" Header="Y" />
<DataGridTextColumn Binding="{Binding Z}" Header="Z" />
</DataGrid.Columns>
</DataGrid>
Edit
Мне просто пришло в голову, что, возможно, проблема в том, что я ссылаюсь на неправильную версию инфраструктуры Aero.
Вот что у меня сейчас:
<ResourceDictionary
Source="/PresentationFramework.Aero,
Version=3.0.0.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
Должно ли быть обновлено до версии 4.0? Что такое PublicKeyToken
для версии 4 (или как это понять)?