删除右键单击事件的 ListBoxItem 背景

Removing ListBoxItem background for right click events

我已经设法阻止 ListBox 项目背景在悬停和选择时显示,但是,当我右键单击一个项目时它会出现,然后甚至在悬停时显示背景 ListBox再次获得焦点(对于之前右键单击的元素)。

我无法在 ListBoxPreviewMouseDown 中使用 e.Handled = true,因为这会破坏我的右键单击上下文菜单。所谓中断,我的意思是它可以防止菜单项对点击做出反应。上下文菜单显示正常,但无法调用项目点击。

我缺少什么来覆盖右键单击时不显示的 ListBoxItem 背景?

谢谢。

<!-- Style -->
<Style TargetType="ListBoxItem" x:Key="MyListBoxItemStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}"
                SnapsToDevicePixels="True">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                Content="{TemplateBinding Content}"
                                ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                        <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />

                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="False" />
                            <Condition Property="IsSelected" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                        <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True" />
                            <Condition Property="IsSelected" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                        <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- Listbox -->

<ListBox Name="ListBoxOne">
        <ListBox.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="ListBox.IsMouseOver" Value="True">
                        <Setter 
                            Property="ListBox.ItemContainerStyle" 
                            Value="{StaticResource MyListBoxItemStyle}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </userControls:ListBox>

只需始终将 ListBoxItemContainerStyle 属性 设置为您的自定义样式,即不要将其设置在 Style 内的触发器中:

<ListBox Name="ListBoxOne">
    <ListBox.Style>
        <Style TargetType="ListBox">
            <Setter Property="ItemContainerStyle" Value="{StaticResource MyListBoxItemStyle}"/>
        </Style>
    </ListBox.Style>
</ListBox>