【发布时间】:2019-06-10 01:06:38
【问题描述】:
我有一个装满苹果的 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 正在应用灰色背景颜色。但是有一个不应该存在的白色边框。如果仔细观察,在边框内的盒子左右两侧也有细微的灰色带。 (悲伤的脸)
有人知道我的数据模板或列表框设置有什么问题吗?
【问题讨论】: