Remove black border style from ListView Selected item

耗尽温柔 提交于 2020-01-06 19:41:25

问题


I am trying to create a custom control like the one described in at: https://stackoverflow.com/a/13188979/637142

So far I have a listview as:

<ListView Name="listBox1">

        <!-- Place items horizontaly -->
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" ></StackPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Style.Resources>                        
                    <!-- Background for Selected ListViewItem -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                             Color="Yellow"/>
                </Style.Resources>
            </Style>
        </ListView.ItemContainerStyle>

        <!-- The items on the listview  -->
        <ListView.Items>
            <TextBlock Margin="5">Test1</TextBlock>
            <TextBlock Margin="5">Test2</TextBlock>
            <TextBlock Margin="5">Test3</TextBlock>
        </ListView.Items>

</ListView>

The only problem that I have now is when the user selects a item with the arrow keys. For example if I select a item with the mouse this is how it looks:

But if I select the same item with the arrow keys this is how it looks:

How can I remove the black dotted border from the selected item!

I do not want to add the previewKeyDown event and then handle it like

 if (e.Key == Key.Left)
 {
         listBox1.SelectedIndex--;
         e.Handled = true;
 }
 else if (e.Key == Key.Right)
 {
         listBox1.SelectedIndex++;
         e.Handled = true;
 }

Because I will also like to be able to select multiple items with the shift key.


回答1:


ListViewItem has a property called FocusVisualStyle that defines the border.

You can simply set the property to null to remove the border:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Resources>

            <!-- Background for Selected ListViewItem -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                        Color="Yellow"/>
        </Style.Resources>

        <!-- Make sure the dotted border is never shown on ListViewItem -->
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</ListView.ItemContainerStyle>


来源:https://stackoverflow.com/questions/13198580/remove-black-border-style-from-listview-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!