Hi,
in these days I needed to override the default highlight color used for marking selected items in GridView of WPF Application running on Windows 7.
First of all I noticed that the well known strategy that consist in overriding the SystemColors.HighlightBrushKey didn’t work for ListView that contains GridView on Windows 7 even if the same strategy works fine in ListBox and ListView (that does not have GridView inside) on both Windows XP and 7. This strange behavior is also described in the following msdn post: http://social.msdn.microsoft.com/Forums/en/wpf/thread/1f62b66f-bdcb-4092-8568-a8fa4d39ea9b.

In order to solve that problem, my idea was to define a global style for ListViewItem, but ListViewItem Style  must be different for ListView that contains GridView and ListView without GridView.
In fact when a ListView contains a GridView, the ListViewItem Style must display items via GridViewRowPresenter.
Instead when ListView does not contains the GridView, the ListViewItem Style must display item via ContentPresenter.
This implies to implement a Trigger into ListViewItem Style in order to hide\show the GridViewRowPresenter or the ContentPresenter.

Below a screenshot of ListView and ListView + GridView using a common ListViewItem Style! The solution is downloadable here.

Highlighted item in ListView via ListViewItem Style

And here you are the final working solution for defining a common ListViewItem Style. Notice the Triggers in lines 36-38 that shows/hides the ContentPresenter.

        <Style TargetType="{x:Type ListViewItem}" x:Key="{x:Type ListViewItem}" >
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">

                        <Border SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Background="{TemplateBinding Background}">

                            <Grid>

                                <!-- This is used when GridView is put inside the ListView -->
                                <GridViewRowPresenter Content="{TemplateBinding ContentControl.Content}"
                                                  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
                                                  VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                                                  SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>

                                <!-- This is used for ListView that does not use GridView -->
                                <ContentPresenter x:Name="contentPresenter"
                                              Content="{TemplateBinding ContentControl.Content}" 
                                              Visibility="Collapsed"
                                              ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
                                              ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
                                              HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
                                              VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                                              SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
                            </Grid>

                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
                                <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible"/>
                            </Trigger>

                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#FFB3D0E5" />
                            </Trigger>

                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                            </Trigger>
                        </ControlTemplate.Triggers>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Stay tuned!
Marzio.

Advertisements