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

标签 c# wpf xaml listbox listboxitem

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

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

我还缺少什么来覆盖右键单击时不显示的 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>

关于c# - 删除右键单击事件的 ListBoxItem 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42846515/

相关文章:

Silverlight工具包图表: Assign Hyperlink to Axis

c# - C# 5.0异步中的awaitable和awaiter

c# - 在 MVC Razor 中访问 WCF 服务

wpf - 将 WPF 模板添加到类库项目

c# wpf - 多线程

c# - 如何在单击事件中设置 Canvas ZIndex WPF 按钮控件?

c# - 使用 Serilog 在 WebAPI 2 中记录重要信息

c# - Azure函数应用程序-写入blob

.net - 构建同步框架 DAL 的正确方法

c# - 从另一个页面读取 IsolatedStorage 中的设置