Я не уверен, что происходит в вашем случае, но немного подозрительно, что ваш ContentTemplate
никогда не определяет ContentPresenter
. Во всяком случае, я думаю, что есть другой способ достичь вашей цели. Попробуйте следующий XAML:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource BluePrimaryBrush}" />
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource TransparentOverlay1Brush}">
<Rectangle.Style>
<Style TargetType="Rectangle">
<!-- Don't show overlay when not selected -->
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="True">
<!-- Show overlay when selected -->
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<Image Grid.Row="0">
<Image.Style>
<Style TargetType="Image">
<!-- Use Image when not selected -->
<Setter Property="Source" Value="{Binding Image}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="True">
<!-- Use SelectedImage when selected -->
<Setter Property="Source" Value="{Binding SelectedImage}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Grid.Row="1" Text="{Binding Title}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<!-- Remove default selection highlighting -->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Вы определяете ItemTemplate
, и в зависимости от того, выбран ли ListBoxItem
, в котором он находится, вы выбираете, использовать ли оверлей и свойство SelectedImage
. ListBoxItem.ControlTemplate
заменяется пустым ContentPresenter
, чтобы удалить все выделения по умолчанию (поскольку вы делаете это самостоятельно).