我有一个装满苹果的ListBox。我想要将选定的项目更改为只有一个没有边框的纯色背景。我遵循了这个建议:
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>当我运行这个程序,并选择一个苹果时,我得到的结果是:

您可以看到XAML正在应用灰色背景色。但是有一条白色的边框不应该出现在那里。如果你仔细观察,在边框内部的左边和右边也有一些微妙的灰色条纹。(悲伤的脸)
有人知道我的数据模板或ListBox设置出了什么问题吗?
发布于 2019-01-16 10:50:31
如果您只想删除所选项目的边框,请尝试以下操作:
在触发器IsSelected中添加<Setter Property="BorderThickness" Value="0"/>。
在我的演示中,我发现它是有效的。
发布于 2019-01-17 02:23:38
MarkFeldman的答案起作用了,但存在鼠标点击事件问题。使用边框元素上的填充,当在文本项之间的填充区域中单击时,鼠标单击将不会注册。为了解决这个问题,我用StackPanel替换了边框,并将填充移动到了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>
https://stackoverflow.com/questions/54208704
复制相似问题