WPF ListBox 如何关闭鼠标悬停效果

12
我在互联网上找到并尝试了许多解决方案,应该可以让我禁用 WPF ListBox 的悬停效果,但是它们似乎都不适用于我。这个截图显示了我想隐藏或去掉的悬停效果:

hover effect

下面是我当前所拥有的 XAML 代码(一个简化版本):
<ListBox ItemsSource="{Binding Logs}" Grid.Column="1" Width="800"  Height="100" >
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="Control.IsMouseOver" Value="True">
                    <Setter Property="Control.Background" Value="Transparent" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

然而,由于某种原因,它似乎对我没有起作用。我的父ListBox(背景中的ListBox)或其他控件有可能覆盖其子控件的样式吗?(我已经尝试过覆盖父元素的样式了)


3
我刚刚创建了一个只有列表框的WPF应用程序,但默认情况下没有悬停效果。 - Tomtom
这相当令人困惑。我已在Windows 8上进行了测试(如屏幕截图所示),以及在Windows 7上进行了测试,其中悬停效果显示为更丑陋的蓝色...我将尝试创建一个新的WPF应用程序,其中只包含一个列表框,以验证它是否有所不同。 - beta
是的,你说得对,ListBox 默认情况下甚至没有悬停效果。所以我猜这是因为我们为所有子控件设置了全局样式。有没有办法覆盖这个样式?我目前正在尝试使用 <Setter Property="OverridesDefaultStyle" Value="true"/> 开始工作。 - beta
在这里发布您的默认样式。也许我们可以帮助您。 - Tomtom
如果您只是想在用户界面上简单地显示一些列表项,那么最好使用ItemsControl。 - Shah Aadil
我假设当你说不想要悬停效果时,你不需要选择该项。如果是这样,请使用ItemsControl替换你的ListBox。 - Vimal CK
2个回答

27

修改 ListBoxItem 的默认样式

<Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="2,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

只需添加一个触发器

<Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Transparent"/>
                        </Trigger>

15
为什么不使用<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">,而是重新定义整个样式并在其上添加新的内容呢? - hyankov

2

摆脱蓝色鬼怪?

如果您不需要单击项目,则以下内容将使其消失,即鼠标悬停时的高亮显示:

<ListBox IsHitTestVisible="False">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsTabStop" Value="False" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem Content="Glad that" />
    <ListBoxItem Content="they are over" />
    <ListBoxItem Content="the days of the blue devil" />
</ListBox>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接