0

I have a ListView with custom ItemTemplate for the items. I want to remove or change the visual effect for selection.

So far I tried assgining a custom ItemContainerStyle to my ListView:

<ListView x:Name="DispList" ItemContainerStyle="{StaticResource MySty}" ItemTemplate="{StaticResource Mine}">

</ListView>

And under resources, defining the style as follows:

<Style TargetType="{x:Type ListViewItem}" x:Key="MySty">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
        </Style.Resources>
</Style>

Unfortunately, this does not work. What am I missing?

1 Answer 1

2

This approach of trying to override the system colours doesn't work on Windows 8 and later. You need to modify the ControlTemplate of the ListViewItem container.

Refer to: ListView Selected Item Style Override

An example:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="RootBorder">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="RootBorder" Property="BorderBrush" Value="Red"/>
                                <Setter TargetName="RootBorder" Property="BorderThickness" Value="1"/>
                            </Trigger>
                            <!--<Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="RootBorder" Property="Background" Value="LightBlue"/>
                            </Trigger>-->
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    <ListViewItem>123</ListViewItem>
    <ListViewItem>456</ListViewItem>
    <ListViewItem>789</ListViewItem>
</ListView>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing in the right direction, but could you please elaborate what properties under ControlTemplate correspond to the visual selection effect? Sorry, I just want to disable the selection visibility.
I have modified my answer by adding an example which can achieve what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.