应用ItemContainerStyle时ListView丢失GridView显示

时间:2012-06-06 20:52:42

标签: wpf listview gridview itemcontainerstyle

我有一个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。 Style是我用于List Box 的样式的几乎完全副本,除了用ListViewItem替换ListBoxItem:

                    <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风格?谢谢!

1 个答案:

答案 0 :(得分:9)

尝试将您样式中的Content Presenter更改为GridViewRowPresenter

<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">
        <GridViewRowPresenter />
    </Border>
</Border>