开发者

Why did my DataGrid styling break when upgrading from .NET 3.5 w/ WPF Toolkit to .NET 4.0?

开发者 https://www.devze.com 2023-01-25 22:23 出处:网络
I just converted a WPF project from .NET 3.5 to .NET 4.0. I\'m now using the .NET 4.0 DataGrid control rather than the WPF Toolkit DataGrid control. Functionally, everything is still working, but my

I just converted a WPF project from .NET 3.5 to .NET 4.0.

I'm now using the .NET 4.0 DataGrid control rather than the WPF Toolkit DataGrid control. Functionally, everything is still working, but my styles are not applying as expected.

As you can see from the below screen captures, the alternating row formatting, padding, bol开发者_高级运维d headings, etc. have stopped working.


Before (WPF Toolkit DataGrid)

Why did my DataGrid styling break when upgrading from .NET 3.5 w/ WPF Toolkit to .NET 4.0?

After (.NET 4.0 DataGrid)

Why did my DataGrid styling break when upgrading from .NET 3.5 w/ WPF Toolkit to .NET 4.0?


Here is my entire resource dictionary.

<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>

Here is a usage example (note that the style is set to "DataGrid_FixedStyle"):

<DataGrid
    Style="{StaticResource DataGrid_FixedStyle}"
    Grid.Column="0" Foreground="Black"
    SelectedIndex="{Binding SelectedParticipantIndex, Mode=TwoWay}"
    ItemsSource="{Binding Participants}">
    <DataGrid.Columns>
        <DataGridTextColumn Foreground="Black" Header="Participant" Binding="{Binding ParticipantId}" />
        ....
    </DataGrid.Columns>
</DataGrid>

Note

To make sure the resource dictionary was really being used, I added the following setter to the <Style TargetType="DataGrid">...</Style>:

<Setter Property="FontSize" Value="24" />

As you can see from the screen capture below, the font size is cartoonishly large, so the style itself is definitely not being ignored. The problem is that many of the settings are not being used or not working for some reason.

Why did my DataGrid styling break when upgrading from .NET 3.5 w/ WPF Toolkit to .NET 4.0?


Any theory on what might have caused my styles to break?


I think I found the culprit. In my App.xaml, I apply the "Aero" theme using the following declaration:

<ResourceDictionary
    Source="/PresentationFramework.Aero,
    Version=3.0.0.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35,
    ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />

After that, I include the resource dictionary that performs additional styling on the DataGrid using the following declaration:

<ResourceDictionary
    Source="/CommonLibraryWpf;component/ResourceDictionaries/DataGridResourceDictionary.xaml" />

If I remove the Aero theme, the custom styling applies correctly (although it loses its Aero look since I'm running this on Windows XP). This problem definitely didn't occur in WPF 3.5, though. I'm not sure what exactly has changed between .NET 3.5 and 4.0 that would make this fail.

Now I just have to figure out how to get the Aero theme and the custom DataGrid styling to work at the same time :)

Edit

Please see this followup question.

0

精彩评论

暂无评论...
验证码 换一张
取 消