【问题标题】:Changing background of listbox item on mouse over在鼠标悬停时更改列表框项目的背景
【发布时间】:2014-09-25 03:31:56
【问题描述】:

我的列表框项目的样式如下:

<Style TargetType="ListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#007acc"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#007acc"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="WhiteSmoke"/>            
    </Style.Resources>
    <Setter Property="Padding" Value="5,2,5,2"/>
    <Setter Property="Margin" Value="0"/>
</Style>

但我不知道如何在鼠标光标悬停在列表框项上时更改其背景颜色。

【问题讨论】:

    标签: c# wpf xaml windows-7


    【解决方案1】:

    使用MouseEnterMouseLeave 事件。

    http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseenter(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleave(v=vs.110).aspx

    MouseEnter 事件中,更改背景颜色。在MouseLeave 上,恢复为“正常”颜色。这应该会产生您想要的效果。

    【讨论】:

      【解决方案2】:

      要达到您想要的效果,请在 ListBoxItem 的 IsMouseOver 属性上使用Trigger 的样式。 IsMouseOver 是一个布尔属性,当鼠标悬停在 ListBox 项上时,它会自动设置为 true。 (阅读MSDN documentation for IsMouseOver 以更好地了解此属性的工作原理。)

      带有触发器的样式的 XAML 如下所示:

      <Style TargetType="ListBoxItem">
          ...
      
          <Style.Triggers>
              <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background" Value="Red"/>
              </Trigger>
          </Style.Triggers>
      </Style>
      

      (如果您查看MSDN documentation for Trigger,您会注意到我在此处给出的示例与文档中给出的示例看起来并没有太大不同...)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多