1

How do I change the color of the item focus rectangle on a WPF ListView?

It's the color of the focus rectangle I mean to change, not the color of the selected item (which is green).

See image: enter image description here

viking_grll
  • 131
  • 8
  • already answered , hope this is you are looking. https://stackoverflow.com/questions/14838524/how-to-set-a-wpf-listview-selected-item-color – mbshambharkar Dec 27 '18 at 16:23
  • no that topic you talk about is about changing the background color... I know how to do that. I want to change the focus rectangle color. – viking_grll Dec 27 '18 at 16:26
  • 2
    You would need to change the style of the focus visual (the focus visual style itself being a property of the ItemContainerStyle). Here is an example of how to manipulate the focus visual style when using a listbox, but it should give you a guidance and insight of how you can do it in your case: https://social.msdn.microsoft.com/Forums/vstudio/en-US/94b33a20-3d8e-4e13-aca5-76547509978c/how-to-change-the-focus-visual-style-of-a-listbox-thats-using-a-datatemplate?forum=wpf –  Dec 27 '18 at 16:28
  • @elgonzo You win ;-) post it as an answer so I can give your credit please. – viking_grll Dec 27 '18 at 16:34

1 Answers1

3

To change the appearance of the focus visual of an item in the ListView, you will have to manipulate the FocusVisualStyle that is part of the ItemContainerStyle used by the ListView.

To illustrate, here is a simple example that turns the focus visual into a red dashed (dotted line) rectangle:

<ListView ...>
    <ListView.ItemContainerStyle>

        <!-- This is the style used by the item container of this ListView -->
        <Style TargetType="ListViewItem">
            <Setter Property="FocusVisualStyle">
                <Setter.Value>

                    <!-- This is the style for the focus visual -->
                    <Style>
                        <Setter Property="Control.Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Rectangle SnapsToDevicePixels="true"
                                               Stroke="Red"
                                               StrokeThickness="1"
                                               StrokeDashArray="1 2"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

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

    </ListView.ItemContainerStyle>
</ListView>