我有一个 WPF 应用程序,我在其中设置了几个组件的样式,使其看起来更像默认的 Windows 7 样式。现在我想对 ListView 做同样的事情,因为默认样式(即使在 Windows 7 上)看起来不太相似。
ListView 的 View 设置为带有一些 GridViewColumns 的 GridView。未应用任何样式的 ListView 的 XAML 如下所示:
<ListView ItemsSource="{Binding Series.ServiceSeries.Weeks}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="" DisplayMemberBinding="{Binding WeekNumber}" />
<GridViewColumn Header="Start date" DisplayMemberBinding="{Binding Path=., Converter={StaticResource WeekConverter}, ConverterParameter=start}" />
<GridViewColumn Header="Track" DisplayMemberBinding="{Binding Path=., Converter={StaticResource WeekConverter}, ConverterParameter=track}" />
<GridViewColumn Header="Race length" DisplayMemberBinding="{Binding Path=., Converter={StaticResource WeekConverter}, ConverterParameter=length}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
它在 Windows 7 上看起来像这样,您可以看到颜色略有错误: 1 http://www.nickthissen.nl/Images/Persistent/tmp3DCB.png
所以我决定对 ListView 的 ItemContainerStyle 应用自定义样式。除了用 ListViewItem 替换 ListBoxItem 之外,样式几乎与我用于 List Box的样式完全相同:
<Style TargetType="{x:Type ListViewItem}" x:Key="ListViewItemStyle">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Border" CornerRadius="3" BorderThickness="1" SnapsToDevicePixels="True">
<Border x:Name="InnerBorder" CornerRadius="2" BorderBrush="Transparent" Background="Transparent"
BorderThickness="1" Padding="3,1" SnapsToDevicePixels="True">
<ContentPresenter />
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource ListboxItemHoverBorder}" />
<Setter TargetName="InnerBorder" Property="BorderBrush"
Value="{StaticResource ListboxItemHoverInnerBorder}" />
<Setter TargetName="InnerBorder" Property="Background"
Value="{StaticResource ListboxItemHoverBackground}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource ListboxItemSelectedBorder}" />
<Setter TargetName="InnerBorder" Property="BorderBrush"
Value="{StaticResource ListboxItemSelectedInnerBorder}" />
<Setter TargetName="InnerBorder" Property="Background"
Value="{StaticResource ListboxItemSelectedBackground}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter TargetName="Border" Property="BorderBrush"
Value="{StaticResource ListboxItemInactiveBorder}" />
<Setter TargetName="InnerBorder" Property="BorderBrush"
Value="{StaticResource ListboxItemInactiveInnerBorder}" />
<Setter TargetName="InnerBorder" Property="Background"
Value="{StaticResource ListboxItemInactiveBackground}" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
将此样式应用于 ListView 的 ItemContainerStyle 属性后,ListView 如下所示: 2 http://www.nickthissen.nl/Images/Persistent/tmpCCB2.png
如您所见,颜色现在好多了,但它完全失去了它的 GridView 显示样式,而是完全忽略了列,只是插入了对象的 ToString 表示(我没有覆盖它,所以它返回了完整的类型名称)。
我不明白为什么会发生这种情况,除了删除 ItemContainerStyle 之外,我没有做任何事情来摆脱它。我的风格一定有问题,但我找不到问题...
谁能看到我做错了什么以及如何恢复我的 GridView 样式?谢谢!