【问题标题】:Remove black border style from ListView Selected item从 ListView 选定项中删除黑色边框样式
【发布时间】:2012-11-02 15:44:44
【问题描述】:

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

到目前为止,我有一个列表视图:

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

我现在唯一的问题是当用户使用箭头键选择一个项目时。例如,如果我用鼠标选择一个项目,它的外观是这样的:

但如果我用箭头键选择相同的项目,它的外观是这样的:

如何去除所选项目的黑色虚线边框!

我不想添加 previewKeyDown 事件然后像处理它

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

因为我还希望能够使用 shift 键选择多个项目。

【问题讨论】:

    标签: c# wpf listview styles


    【解决方案1】:

    ListViewItem 有一个名为FocusVisualStyle 的属性来定义边框。

    您可以简单地将属性设置为 null 以移除边框:

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

    【讨论】:

    • 覆盖 SystemColors.HighlightBrushKey 对我没有任何帮助,但关闭 FocusVisualStyle 效果很好!
    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 2019-11-17
    • 2020-09-10
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多