代码之家  ›  专栏  ›  技术社区  ›  mfanto

ListBoxItem中IsMouseOver on按钮出现奇怪问题

  •  0
  • mfanto  · 技术社区  · 15 年前

    我有一个列表框项目的模板。它包括一个图像按钮。鼠标悬停时,该按钮会更改图像。我遇到的问题是,当我悬停时,它在按钮上添加了一个奇怪的工件。但只有在ListBoxItem中才会发生这种情况。如果我把按钮放在主画布上,它可以正常工作。

    以下是正常状态下的图像:

    alt text

    这是处于悬停状态的图像:请注意顶部和右侧边缘上的两条白线。

    alt text

    alt text

    这是我按钮的代码。我尝试过使用每个背景笔刷(按钮、列表框项目等)

        <Style x:Key="RedXButton" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Foreground" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Image Width="19" Height="18">
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Setter Property="Source" Value="Views/Images/RedX.png"/>
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Source" Value="Views/Images/RedXHover.png"/>
                                                </Trigger>
                                            </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true"/>
                            <Trigger Property="ToggleButton.IsChecked" Value="true"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Width" Value="19"/>
            <Setter Property="Height" Value="18"/>
            <Setter Property="OpacityMask" Value="{x:Null}"/>
        </Style>
    

    我的列表框项目模板:

    <Canvas x:Name="LayoutRoot">
            <Image x:Name="image" Source="/Views/Images/FileGradient.png" Width="375" Height="43"/>
            <Button x:Name="RedX" Style="{DynamicResource RedXButton}" Command="{Binding RemoveCommand}" Canvas.Left="11" Canvas.Top="13" Width="19" Height="18" />
        </Canvas>
    
    2 回复  |  直到 13 年前
        1
  •  0
  •   Conan    15 年前

    只是想一想——试着设置 SnapsToDevicePixels 属性设置为“true”。也可能值得将其设置为按钮样式:

    <Setter Property="SnapsToDevicePixels" Value="true"/>
    
        2
  •  0
  •   mfanto    13 年前

    结果是不同元素的多种样式相互冲突。我不确定实际的修复是什么。我只是简化了所有的样式并重新创建了一些控件,然后它就消失了。不幸的是,我不能再添加更多,因为我不确定最终是什么解决了它。