I have a ListBox filled with Apples. I wanted to change the selected item to only have a solid background with no border. I followed this suggestion:
Question #146269: Change Wpf Datatemplate for Listbox Item if Selected
Here is my 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>
When I run the program, and select an apple, I get this:
You can see that the XAML is working to apply a grey background color. But there's a white border that shouldn't be there. If you look closely, there are also subtle grey bands on the left and right sides of the box just inside the border. (sad face)
Does anyone know what's wrong with my Data Template or my ListBox settings?