WPF ListBoxItem IsMouseOver

发布于 2024-11-29 23:10:22 字数 981 浏览 0 评论 0原文

我有一个列表框,将鼠标悬停在某个项目上时会显示该项目的删除按钮。问题是 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 技术交流群。

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

发布评论

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

评论(2

牵你的手,一向走下去 2024-12-06 23:10:22

您的每一项都将包含在 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 the listBoxItem via the ListBox.ItemContainerStyle property. Move your trigger to the item container and it should work as desired.

还给你自由 2024-12-06 23:10:22

您可以直接在按钮上使用 DataTrigger (或尝试在其所在位置应用相同的 RelativeSource 绑定):

<Style TargetType="{x:Type Button}">
    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                 Value="True">
          <!-- ... -->
    </DataTrigger>
</Style>

You could use a DataTrigger directly on the button (or try to apply the same RelativeSource binding in the place it is):

<Style TargetType="{x:Type Button}">
    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                 Value="True">
          <!-- ... -->
    </DataTrigger>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文