从ListView选定的项目中删除黑色边框样式

从ListView选定的项目中删除黑色边框样式

本文介绍了从ListView选定的项目中删除黑色边框样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义控件,如以下内容中所述: https://stackoverflow.com/a/13188979/637142

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:

如何从所选项目中删除黑色虚线边框!

我不想添加previewKeyDown事件,然后像对待

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;
 }

因为我也希望能够使用Shift键选择多个项目.

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

推荐答案

ListViewItem具有一个名为FocusVisualStyle的属性,用于定义边框.

ListViewItem has a property called FocusVisualStyle that defines the border.

您只需将属性设置为null即可删除边框:

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>

这篇关于从ListView选定的项目中删除黑色边框样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:17