如何去除 WPF 中列表框选定项周围的边框?

How to Get Rid of Border around ListBox Selected Item in WPF?

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

Question #146269: Change Wpf Datatemplate for Listbox Item if Selected

这是我的 xaml:

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

当我 运行 程序和 select 一个苹果时,我得到这个:

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

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

如果您只想删除所选项目的边框,试试这个:

在触发器"IsSelected"中添加<Setter Property="BorderThickness" Value="0"/>

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

MarkFeldman 的答案有效,但存在鼠标单击事件问题。使用 Border 元素上的 Padding,在文本项之间的填充区域中单击时鼠标单击不会注册。为了解决这个问题,我用 StackPanel 替换了 Border,并将 Padding 移到了 TextBlock 上。在文本块上填充本身会正确注册点击。

决赛 xaml:

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