Resetting the overridden SystemColors

111 Views Asked by At

I am changing the SystemColors.HighlightBrushKey and SystemColors.ControlBrushKey in my ListView and it works well; but this list view contains other complex controls in each of it's ListViewItem (e.g. other ListView, DataGrid) and this new system color gets applied to all the child controls.

<!--  This resource is added to remove the blue highlighting 
      in the selected ListView item.  -->
<SolidColorBrush
        x:Key="{x:Static SystemColors.HighlightBrushKey}"
        Color="Transparent" />

<!--  This resource is added to remove the background highlighting 
      of the inactive selected ListView item.  -->
<SolidColorBrush
        x:Key="{x:Static SystemColors.ControlBrushKey}"
        Color="Transparent" />

Is there a way to reset this system color to original one for a control which is a child of this ListView?

The actual problem I am facing is that these overridden system colors are getting applied to the context menu's of child controls (of ListView); This works fine as such but when Windows Classic theme is used ContextMenus use these system colors and look weird. So I am hoping that if I can reset the SystemColors to original ones, then ContextMenu will work properly.

Is there any other way to solve this problem?

1

There are 1 best solutions below

2
On

You should override default template of ListViewItem instead of overriding of SystemColors.

Override the template and remove the triggers of selection and mouseOver from it like this:

    <ListView>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                                    Padding="{TemplateBinding Control.Padding}"
                                    BorderBrush="{TemplateBinding Border.BorderBrush}"
                                    Background="{TemplateBinding Panel.Background}"
                                    Name="Bd"
                                    SnapsToDevicePixels="True">
                                <ContentPresenter Content="{TemplateBinding ContentControl.Content}"
                                                  ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                                  ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                                  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                                  SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="UIElement.IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" TargetName="Bd">
                                        <Setter.Value>
                                            <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

If want to apply on multiple ListView's, put this resource under App.Resources and use wherever needed.