wpf - 如何摆脱 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 正在应用灰色背景颜色。但是有一个不应该存在的白色边框。如果仔细观察,边框内的盒子左右两侧也有细微的灰色带。(悲伤的脸)
有谁知道我的数据模板或列表框设置有什么问题?
解决方案
如果您只想删除所选项目的边框,请尝试以下操作:
添加<Setter Property="BorderThickness" Value="0"/>
触发器“IsSelected”。
在我的演示中,我发现它有效。
推荐阅读
- javascript - 如何获取被调用函数从其他函数获取的返回值?
- python-3.x - 处理 AWS lambda 重试
- javascript - 如何使用 jquery 将类添加到元素的特定兄弟姐妹?
- javascript - 在 Typescript 的静态方法中访问类类型参数的解决方法
- shiny - Shiny 在渲染过程中生成的 CSS 和 Javascript 文件在哪里?
- python - 哪些版本的 TensorFlow 可以与哪些版本的 CUDA 配合使用
- python - matplotlib plt.Rectangle 的奇怪行为
- google-cloud-build - 在合并时部署,而不是在使用 Google Cloud Build 的拉取请求时部署
- java - 如何将空数组列表添加到 ListView?尝试在空对象引用上调用接口方法“int java.util.List.size()”
- laravel - laravel Policy 参数太少而无法运行