WPF ListViewItem.IsMouseOver 不会为整行返回 True

发布于 2024-11-28 13:10:53 字数 1994 浏览 2 评论 0原文

我有一个简单的 WPF ListView ,定义了两列。默认情况下,当您将鼠标移动到行的任何部分上时,它将以跟踪外观显示该行(我使用带有 Aero 的 Windows 7)。下面是一个简单示例的 Xaml,显示此功能按预期工作...

  <Page
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:sys="clr-namespace:System;assembly=mscorlib">
     <Grid>  
       <ListView>
         <ListView.View>
           <GridView>
             <GridViewColumn Header="First" Width="200"/>
             <GridViewColumn Header="Second" Width="200"/>
           </GridView>
         </ListView.View>
         <sys:DateTime>1/2/3</sys:DateTime>
         <sys:DateTime>4/5/6</sys:DateTime>
         <sys:DateTime>7/8/9</sys:DateTime>
         <sys:DateTime>10/11/12</sys:DateTime>
      </ListView>
     </Grid>
   </Page>

我想覆盖 ListViewItem 样式并在跟踪时自定义行的外观。这是我添加到上面的 ListView 中的示例 Xaml...

  <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="ListBoxItem">
            <Border x:Name="Bd">
              <GridViewRowPresenter />
            </Border>
            <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter TargetName="Bd" Property="Background" Value="Red"/>
            </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ListView.ItemContainerStyle>

当鼠标直接位于两列之一内的文本上方时,它仅将行显示为跟踪。如果鼠标位于绘制的文本之间或超出最后一列的右侧,则不会显示为跟踪。

似乎 IsMouseOver 触发器仅在鼠标下方有实际内容时才返回 True,而当我希望它在鼠标位于行的任何部分上方时返回 True 时,即使该行的该区域也是如此恰好是空的。关于如何解决这个问题有什么想法吗?

I have a simple WPF ListView with two columns defined. By default when you move the mouse over any part of a row it will show that row in a tracking appearance (I am using Windows 7 with Aero). Here is the Xaml for a simple example showing this working as expected...

  <Page
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:sys="clr-namespace:System;assembly=mscorlib">
     <Grid>  
       <ListView>
         <ListView.View>
           <GridView>
             <GridViewColumn Header="First" Width="200"/>
             <GridViewColumn Header="Second" Width="200"/>
           </GridView>
         </ListView.View>
         <sys:DateTime>1/2/3</sys:DateTime>
         <sys:DateTime>4/5/6</sys:DateTime>
         <sys:DateTime>7/8/9</sys:DateTime>
         <sys:DateTime>10/11/12</sys:DateTime>
      </ListView>
     </Grid>
   </Page>

I want to override the ListViewItem style and customize the appearance of the row when tracking. Here is the sample Xaml I have added to the above ListView...

  <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="ListBoxItem">
            <Border x:Name="Bd">
              <GridViewRowPresenter />
            </Border>
            <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter TargetName="Bd" Property="Background" Value="Red"/>
            </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ListView.ItemContainerStyle>

It only shows the row as tracking when the mouse is directly over the text inside one of the two columns. If the mouse is between the drawn text or off the right hand side beyond the last column then it does not show as tracking.

It seems like the IsMouseOver trigger only returns True if there is actual content under the mouse, when I want it to return True if the mouse is over any part of the row, even if that area of the row happens to be empty. Any ideas of how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2024-12-05 13:10:53

将边框 Horizo​​ntalAlignment 设置为 Stretch 并为其指定透明(或默认)背景。这将使鼠标悬停在某些地方以突出显示

Set the border HorizontalAlignment to Stretch and give it a transparent (or otherwise default) background. this will give the mouse something to be over to highlight

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文