WPF ListBoxItem IsMouseOver
我有一个列表框,将鼠标悬停在某个项目上时会显示该项目的删除按钮。问题是 IsMouseOver 会触发突出显示的项目大约 4 个像素,因此当鼠标悬停在多个项目上时,删除按钮似乎不会随着您上下移动,而是会在项目之间的间隙中闪烁。有没有办法让 IsMouseOver 响应整个项目?
<ListBox Name="lstLength" ItemsSource="{Binding Source={StaticResource lengths}}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" Height="22">
<Button DockPanel.Dock="Right" Name="btnDelete" Content="X" Tag="{Binding}" Click="DeleteLength" Visibility="Collapsed" />
<TextBlock Text="{Binding}" />
</DockPanel>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="btnDelete" Property="Visibility" Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a ListBox, which on mousing over an item shows a delete button for that item. The problem is that the IsMouseOver triggers about 4 pixels into the highlighted item, so when mousing over multiple items, instead of the delete button seeming to move up and down with you, it flickers in the gaps between the items. Is there anyway to make IsMouseOver respond to the whole item?
<ListBox Name="lstLength" ItemsSource="{Binding Source={StaticResource lengths}}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" Height="22">
<Button DockPanel.Dock="Right" Name="btnDelete" Content="X" Tag="{Binding}" Click="DeleteLength" Visibility="Collapsed" />
<TextBlock Text="{Binding}" />
</DockPanel>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="btnDelete" Property="Visibility" Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的每一项都将包含在
ListBoxItem
中,这就是每一项之间有约 4 个像素的原因。它还提供突出显示和选择样式。您可以通过 ListBox.ItemContainerStyle 属性。将触发器移至项目容器,它应该按预期工作。Each one of your items will be contained within a
ListBoxItem
, this is what gives the ~4 pixels between each item. It also provides the highlight and selection styling. You can style thelistBoxItem
via the ListBox.ItemContainerStyle property. Move your trigger to the item container and it should work as desired.您可以直接在按钮上使用
DataTrigger
(或尝试在其所在位置应用相同的RelativeSource
绑定):You could use a
DataTrigger
directly on the button (or try to apply the sameRelativeSource
binding in the place it is):