WPF ListViewItem.IsMouseOver 不会为整行返回 True
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将边框 HorizontalAlignment 设置为 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