为什么从.NET 3.5 w / WPF Toolkit升级到.NET 4.0时,我的DataGrid样式中断? [英] Why did my DataGrid styling break when upgrading from .NET 3.5 w/ WPF Toolkit to .NET 4.0?
问题描述
我正在使用.NET 4.0 DataGrid
控件,而不是WPF Toolkit DataGrid
控件。在功能上,一切都仍然有效,但是我的样式没有按预期应用。
从下面的屏幕截图可以看出,交替行格式化,填充,粗体标题等等已停止工作。
之前(WPF Toolkit DataGrid)
之后(.NET 4.0 DataGrid)
这是我的整个资源字典。
< ResourceDictionary xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml>
< Style x:Key =DataGrid_ColumnHeaderStyleTargetType =DataGridColumnHeader>
< Setter Property =FontWeightValue =Bold/>
< Setter Property =TextBlock.TextAlignmentValue =Center/>
< Setter Property =TextBlock.TextWrappingValue =WrapWithOverflow/>
< / Style>
< Style x:Key =DataGrid_CellStyleTargetType =DataGridCell>
< Setter Property =PaddingValue =5,5,5,5/>
< Setter Property =TextBlock.TextAlignmentValue =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 =ColumnHeaderStyleValue ={StaticResource DataGrid_ColumnHeaderStyle}/>
< Setter Property =CellStyleValue ={StaticResource DataGrid_CellStyle}/>
< Setter Property =BackgroundValue =White/>
< Setter Property =AlternatingRowBackgroundValue =#F0F0F0/>
< Setter Property =VerticalGridLinesBrushValue =LightGray/>
< Setter Property =HeadersVisibilityValue =Column/>
< Setter Property =SelectionModeValue =Single/>
< Setter Property =SelectionUnitValue =FullRow/>
< Setter Property =GridLinesVisibilityValue =Vertical/>
< Setter Property =AutoGenerateColumnsValue =False/>
< Setter Property =CanUserAddRowsValue =False/>
< Setter Property =CanUserDeleteRowsValue =False/>
< Setter Property =CanUserReorderColumnsValue =True/>
< Setter Property =CanUserResizeColumnsValue =True/>
< Setter Property =CanUserResizeRowsValue =False/>
< Setter Property =CanUserSortColumnsValue =True/>
< Setter Property =IsReadOnlyValue =True/>
< Setter Property =BorderBrush值=#DDDDDD/>
< Setter属性=HorizontalGridLinesBrush值=#DDDDDD/>
< Setter Property =VerticalGridLinesBrushValue =#DDDDDD/>
< / Style>
< Style x:Key =DataGrid_FixedStyleTargetType =DataGridBasedOn ={StaticResource {x:Type DataGrid}}>
< Setter Property =CanUserReorderColumnsValue =False/>
< Setter Property =CanUserResizeColumnsValue =False/>
< Setter Property =CanUserResizeRowsValue =False/>
< Setter Property =CanUserSortColumnsValue =False/>
< / Style>
< / ResourceDictionary>
这是一个使用示例(请注意,设置为DataGrid_FixedStyle):
< DataGrid
Style ={StaticResource DataGrid_FixedStyle}
Grid.Column =0Foreground =Black
SelectedIndex ={Binding SelectedParticipantIndex,Mode = TwoWay}
ItemsSource ={Binding Participants}>
< DataGrid.Columns>
< DataGridTextColumn Foreground =BlackHeader =ParticipantBinding ={Binding ParticipantId}/>
....
< /DataGrid.Columns>
< / DataGrid>
注意 p>
为了确保资源字典真正被使用,我将以下设置器添加到< Style TargetType =DataGrid> ... < / Style>
:
< Setter Property =FontSizeValue =24 />
从下面的屏幕截图可以看出,字体大小是卡通大的,所以风格本身绝对不被忽视。问题是很多设置没有被使用或者由于某种原因不能正常工作。
有什么可能导致我的风格打破的理论?
我想我发现了罪魁祸首。在我的App.xaml中,我使用以下声明应用Aero主题:
< ResourceDictionary
源=/ PresentationFramework.Aero,
Version = 3.0.0.0,
Culture = neutral,
PublicKeyToken = 31bf3856ad364e35,
ProcessorArchitecture = MSIL; component / themes / aero.normalcolor.xaml />
之后,我将包含在 DataGrid上执行额外样式的资源字典
使用以下声明:
< ResourceDictionary
Source =/ CommonLibraryWpf; component /ResourceDictionaries/DataGridResourceDictionary.xaml/>
如果我删除Aero主题,自定义样式适用正确(尽管自从我失去了Aero外观'在Windows XP上运行它)。但WPF 3.5中并没有出现这个问题。我不知道.NET 3.5和4.0之间究竟发生了什么变化,这将导致失败。
现在我只需弄清楚如何获得Aero主题和自定义 DataGrid
修改
请参阅这个后续问题。
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, bold headings, etc. have stopped working.
Before (WPF Toolkit DataGrid)
After (.NET 4.0 DataGrid)
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.
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.
这篇关于为什么从.NET 3.5 w / WPF Toolkit升级到.NET 4.0时,我的DataGrid样式中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!