首页 > 解决方案 > 如何摆脱 WPF 中列表框选定项周围的边框?

问题描述

我有一个装满苹果的 ListBox。我想将所选项目更改为仅具有无边框的纯色背景。我遵循了这个建议:

问题 #146269:如果选中,更改列表框项目的 Wpf 数据模板

这是我的xml:

<UserControl.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="AppleItemTemplate">
            <Border Opacity="1" Padding="10,5">
                    <TextBlock Foreground="{DynamicResource PrimaryTextColor}">
                    <TextBlock.Text>
                        <Binding Path="DisplayName"/>
                    </TextBlock.Text>
                </TextBlock>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="AppleItemTemplateSelected">
            <Border BorderThickness="0" BorderBrush="Transparent" Padding="10,5" Background="{DynamicResource LeftSidebarBGColorHighlight}">
                <TextBlock Foreground="{DynamicResource PrimaryTextColor}">
                    <TextBlock.Text>
                        <Binding Path="DisplayName"/>
                    </TextBlock.Text>
                </TextBlock>
            </Border>
        </DataTemplate>
        <Style TargetType="{x:Type ListBoxItem}" x:Key="AppleContainerStyle">
            <Setter Property="ContentTemplate" Value="{DynamicResource AppleItemTemplate}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{DynamicResource AppleItemTemplateSelected}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

<ListBox ItemsSource="{Binding Apples}"
         SelectedItem="{Binding SelectedApple}"
         ItemContainerStyle="{StaticResource AppleContainerStyle}"

         Background="{DynamicResource LeftSidebarBGColor}"
         BorderThickness="0"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch"
         >

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

当我运行程序并选择一个苹果时,我得到了这个:

精选苹果

您可以看到 XAML 正在应用灰色背景颜色。但是有一个不应该存在的白色边框。如果仔细观察,边框内的盒子左右两侧也有细微的灰色带。(悲伤的脸)

有谁知道我的数据模板或列表框设置有什么问题?

标签: wpfxamllistbox

解决方案


如果您只想删除所选项目的边框,请尝试以下操作:

添加<Setter Property="BorderThickness" Value="0"/>触发器“IsSelected”。

在我的演示中,我发现它有效。


推荐阅读