MouseEnter 触发器在 ListBox 中的项目上触发不准确

发布于 2024-10-30 16:53:12 字数 2544 浏览 1 评论 0原文

我在 WPF 列表框中看到一个非常奇怪的问题,其中每个项目的 MouseEnter 触发器对于列表框中更靠下的项目的触发不太准确。我的 MouseEnter 触发器会触发一个动画,该动画会增加每个项目的 ScaleTransform。

示例:我有一个包含 10 个项目的列表框。列表框显示前 5 项。我将鼠标悬停在第一个项目上,它缩放得很好。我将鼠标悬停在第二个项目上,并且必须将鼠标放在列表项控件内更远的地方才能看到效果。我将鼠标悬停在第三个项目上,并且必须将鼠标放得更远。当我到达列表中的最后一个(第 5 个)可见项目时,我必须将鼠标放置在控件的中点附近,以便鼠标输入即可开火。然后,我向下滚动到底部查看第 6-10 项。项目#6 悬停得很好。 #7 不太准确,#8 更不准确,依此类推,直到 #10,这是非常不准确的。

因此,列表框中的垂直可见位置会影响每个控件的 MouseEnter 准确性。

编辑:当我将此代码放入干净的 XAML 窗口中时,它工作正常。这一定是我提供的代码之外的一些冲突...如果我找到一些东西,我将提供更多详细信息(或答案)。

关于可能导致这种情况的原因有什么想法吗?

这是我的 ListBox XAML:

<ListBox
    MaxHeight="530"
    ItemsSource="{Binding Appointments}"
    Background="Transparent"
    >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <views:AppointmentControl DataContext="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是我的 AppointmentControl 的样子:

<UserControl Height="155" Width="215">
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="Mouse.MouseEnter">
            <BeginStoryboard>
                <Storyboard TargetProperty="RenderTransform.ScaleX">
                    <DoubleAnimation To="1.2" Duration="0:0:0.050" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Mouse.MouseLeave">
            <BeginStoryboard>
                <Storyboard TargetProperty="RenderTransform.ScaleX">
                    <DoubleAnimation To="1.0" Duration="0:0:0.050" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>

    <UserControl.RenderTransform>
        <ScaleTransform ScaleX="1.0" ScaleY="1.0"></ScaleTransform>
    </UserControl.RenderTransform>

    <Button
        Background="#FFCAEE7D" 
        BorderThickness="1,1,1,3" 
        Padding="0"
        >
        <StackPanel Height="125" Width="180">
            <TextBlock ... />
            <TextBlock ... />

            <ItemsControl>
                ...
            </ItemsControl>
        </StackPanel>
    </Button>
</UserControl>

I'm seeing a very odd problem in a WPF list box where the MouseEnter trigger for each item fires less accurately for items further down in my ListBox. My MouseEnter trigger fires off an animation that increases the ScaleTransform of each item.

Example: I have a list box of 10 items. The list box is displaying the first 5 items. I hover over the first item and it scales fine. I hover over the 2nd item and I have to place the mouse a little further inside the list item control to see the effect. I hover over the third item and I have to place the mouse even further in. By the time I get to the last (5th) visible item in the list I have to place the mouse at about the mid-point of the control for the MouseEnter to fire. Then, I scroll down to the bottom to see items 6-10. Item #6 hovers just fine. #7 is less accurate, #8 even less accurate, and so on down to #10 which is very inaccurate.

Thus is looks like the vertical visible position in the list box affects the MouseEnter accuracy of each control.

EDIT: When I put this code in a clean XAML window it works fine. It must be some conflict outside of the code I have provided... I'll provide more details (or an answer) if I find something.

Any ideas on what might be causing this?

Here is my ListBox XAML:

<ListBox
    MaxHeight="530"
    ItemsSource="{Binding Appointments}"
    Background="Transparent"
    >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <views:AppointmentControl DataContext="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And here is what my AppointmentControl looks like:

<UserControl Height="155" Width="215">
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="Mouse.MouseEnter">
            <BeginStoryboard>
                <Storyboard TargetProperty="RenderTransform.ScaleX">
                    <DoubleAnimation To="1.2" Duration="0:0:0.050" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Mouse.MouseLeave">
            <BeginStoryboard>
                <Storyboard TargetProperty="RenderTransform.ScaleX">
                    <DoubleAnimation To="1.0" Duration="0:0:0.050" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>

    <UserControl.RenderTransform>
        <ScaleTransform ScaleX="1.0" ScaleY="1.0"></ScaleTransform>
    </UserControl.RenderTransform>

    <Button
        Background="#FFCAEE7D" 
        BorderThickness="1,1,1,3" 
        Padding="0"
        >
        <StackPanel Height="125" Width="180">
            <TextBlock ... />
            <TextBlock ... />

            <ItemsControl>
                ...
            </ItemsControl>
        </StackPanel>
    </Button>
</UserControl>

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

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

发布评论

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

评论(2

抹茶夏天i‖ 2024-11-06 16:53:12

如果从第五个元素开始进入 ListBox,会发生什么?还需要走到一半才能看到同样的效果吗?

What happens if you enter the ListBox starting with the fifth element? Do you still need to go halfway down to see the same effect?

情独悲 2024-11-06 16:53:12

问题在于列表框顶部具有更大 z-index 的另一个控件会干扰列表项命中测试。另一个控件有一个据说是透明的区域,但无论出于何种原因它都会干扰鼠标输入。当我删除这个其他控件时,问题就消失了。

The issue was that another control with a greater z-index on top of the list box was interfering with the list item hit testing. The other control had a supposedly transparent area but for whatever reason it interferes with the mouseenter. When I remove this other control the problem goes away.

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