从带有 WPF Toolkit 的 .NET 3.5 升级到 .NET 4.0 时,为什么我的 DataGrid 样式会损坏?
我刚刚将 WPF 项目从 .NET 3.5 转换为 .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_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_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>
注意
为了确保资源字典确实被使用,我将以下设置器添加到 :
<Setter Property="FontSize" Value="24" />
正如您从下面的屏幕截图中看到的,字体大小卡通般大,因此样式本身绝对不能被忽略。问题是许多设置没有被使用或由于某种原因不起作用。
关于什么可能导致我的样式破坏的任何理论?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我找到了罪魁祸首。在我的 App.xaml 中,我使用以下声明应用“Aero”主题:
之后,我使用以下声明包含在
DataGrid
上执行其他样式的资源字典:如果我删除 Aero主题,自定义样式正确应用(尽管它失去了 Aero 外观,因为我在 Windows XP 上运行它)。不过,这个问题在 WPF 3.5 中绝对不会发生。我不确定 .NET 3.5 和 4.0 之间到底发生了什么变化导致此失败。
现在我只需要弄清楚如何让 Aero 主题和自定义
DataGrid
样式同时工作:)编辑
请参阅
I think I found the culprit. In my App.xaml, I apply the "Aero" theme using the following declaration:
After that, I include the resource dictionary that performs additional styling on the
DataGrid
using the following declaration: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.