首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何摆脱WPF中ListBox选中项的边框?

如何摆脱WPF中ListBox选中项的边框?
EN

Stack Overflow用户
提问于 2019-01-16 08:18:45
回答 2查看 1K关注 0票数 0

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

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

下面是我的xaml:

代码语言:javascript
复制
<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正在应用灰色背景色。但是有一条白色的边框不应该出现在那里。如果你仔细观察,在边框内部的左边和右边也有一些微妙的灰色条纹。(悲伤的脸)

有人知道我的数据模板或ListBox设置出了什么问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-16 10:50:31

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2019-01-17 02:23:38

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

最终的xaml:

代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54208704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档