【问题标题】:WPF "tooltip" popup flickersWPF“工具提示”弹出窗口闪烁
【发布时间】:2023-03-06 23:48:01
【问题描述】:

长话短说:我有一个在 ListBox 中显示一堆图表的窗口。当鼠标悬停在图表上(使用 LineSeries)时,有一条线跟随 dataPoints(捕捉到 dataPoint 位置)。在那条线附近,我展示了一个由弹出窗口制作的工具提示,其中显示了有关这些数据点的信息。

到目前为止一切顺利。问题是当我尝试将鼠标移到工具提示上时,弹出窗口开始闪烁(就像它在打开/关闭循环中一样)。我已经在弹出窗口和孩子IsHitTestVisible="False" 上设置了。

作为临时解决方案,弹出窗口“离开”光标的方式,如下所示:

...但很难“理解”。

现在问题来了:怎么了?为什么当鼠标悬停时弹出窗口开始闪烁。

欢迎任何反馈

PS。工具提示 XAML(它是在代码中创建的,但在这里):

图表的数据上下文是绑定到一个类的数据,一些图表的事件也是通过命令实现的。弹出窗口是在该类的构造函数中创建的,

ppchart = New Popup() With {.AllowsTransparency = True, .IsHitTestVisible = False,.StaysOpen = True}

...在 MouseMoveCommand 中创建弹出窗口的子项:

Dim ppCont As XElement = <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" IsHitTestVisible="False" VerticalAlignment="Top">
                                     <Grid.RowDefinitions>
                                         <RowDefinition Height="Auto"/>
                                         <RowDefinition Height="Auto"/>
                                     </Grid.RowDefinitions>
                                     <Rectangle Opacity="0.5" Grid.RowSpan="2" IsHitTestVisible="False" StrokeThickness="0" RadiusX="2" RadiusY="2" Fill="#FFBABABA"/>
                                     <TextBlock Text="{Binding Over, StringFormat=HH:mm}" FontSize="9" TextAlignment="Center" FontFamily="Segoe UI" IsHitTestVisible="False" Margin="1"/>
                                     <ListBox x:Name="listBox" ItemsSource="{Binding Points}" Background="{x:Null}" BorderBrush="{x:Null}" FontSize="8" Margin="1,0,1,1" Grid.Row="1" IsHitTestVisible="False" IsTextSearchEnabled="False" HorizontalAlignment="Stretch">
                                         <ListBox.ItemContainerStyle>
                                             <Style TargetType="{x:Type ListBoxItem}">
                                                 <Setter Property="Background" Value="Transparent"/>
                                                 <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                                                 <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                                                 <Setter Property="Padding" Value="0"/>
                                                 <Setter Property="IsHitTestVisible" Value="False"/>
                                             </Style>
                                         </ListBox.ItemContainerStyle>
                                         <ListBox.ItemTemplate>
                                             <DataTemplate>
                                                 <Grid IsHitTestVisible="False">
                                                     <Grid.ColumnDefinitions>
                                                         <ColumnDefinition Width="Auto"/>
                                                         <ColumnDefinition Width="Auto"/>
                                                         <ColumnDefinition Width="Auto"/>
                                                     </Grid.ColumnDefinitions>
                                                     <Rectangle Fill="{Binding Culoare}" Width="3" HorizontalAlignment="Left" Margin="1" IsHitTestVisible="False"/>
                                                     <TextBlock Text="{Binding Operation}" HorizontalAlignment="Stretch" IsHitTestVisible="False" Grid.ColumnSpan="1" Grid.Column="1"/>
                                                     <TextBlock Text="{Binding points.Value}" HorizontalAlignment="Stretch" Grid.Column="2" TextAlignment="Right" IsHitTestVisible="False"/>
                                                 </Grid>
                                             </DataTemplate>
                                         </ListBox.ItemTemplate>
                                     </ListBox>
                                 </Grid>

        ppchart.Effect = New Effects.DropShadowEffect() With {.Opacity = 0.5, .BlurRadius = 5, .Direction = 80, .Color = Colors.Black}
        ppchart.Child = CType(XamlReader.Load(New XmlTextReader(New StringReader(ppCont.ToString))), UIElement)

重新编辑:这就是它的样子

【问题讨论】:

  • 可以发布用于工具提示的 XAML 吗?
  • 为什么要设置 IsHitTestVisible?你试过了吗?
  • 我第一次尝试完全不设置它,它也在闪烁......
  • 你在什么事件上触发工具提示?
  • 图表的 MouseMove 绑定到一个命令。在该命令中,我正在创建弹出窗口的子项,为其创建数据上下文,并设置 PopUp.IsOpen="True" ...

标签: wpf popup flicker


【解决方案1】:

我在使用 MouseEnterMouseLeave 事件时也遇到了类似的问题。 WPF上的MouseEnter事件有一个奇怪的行为,不知道是不是bug。当您从顶部输入考虑的项目时,会发生闪烁。从任何其他方向输入项目会给您一个流畅的弹出窗口。所以为了避免它,我删除了:

AllowsTransparency = True

部分。

已编辑:

虽然仍然无法找到满足我需要的完美行为,但我发现这实际上并不是 Scott Hanselman here 所描述的错误

但是,我无法将问题与我的问题进行比较并解决。无论如何,为了拥有动画功能,我想出了一个解决方案,通过检查鼠标的当前位置,至少可以减少 Popup 控件的额外打开或关闭。我定义了一个变量

Point currPoint = new Point();

然后在 MouseEnter 事件或处理程序中:

   ListViewItem listViewItem = e.Source as ListViewItem;           
        if(currPoint!=e.GetPosition(listViewItem))
            compToStrategyVM.OpenPopup(listViewItem, listViewPopup);

同样在 MouseLeave 事件或处理程序中:

ListViewItem listViewItem = e.Source as ListViewItem;
        if (currPoint != e.GetPosition(listViewItem))
            compToStrategyVM.ClosePopup(listViewPopup);

这里compToStrategyVM 指的是我的ViewMode,listViewPopup 指的是Popup 控件的名称。虽然问题仍然存在,但通过这种方法,闪烁效果已降低到相当大的一部分。

【讨论】:

  • 唯一的缺点是您不能使用需要将上述变量设置为'true'的弹出动画功能
【解决方案2】:

在阅读了 cmets 之后,我相信您使用了不正确的事件; MouseMove 事件将被持续触发,并且每次弹出窗口显示/隐藏或闪烁。

您想使用MouseEnterMouseLeave 事件。

【讨论】:

  • 我已经试过了,但是我必须检查鼠标的数据点值是否改变了(现在你会说将它附加到数据点的MouseEnterMouseLeave,但是LineSeries 我将数据点的宽度/高度设置为 0,我只需要这条线。无论如何,在 MouseMove 中,我在“重新打开”之前检查 PopUp 的值是否不同...
  • 有趣的事情。当鼠标悬停在 ToolTip 上时,ChartMouseLeave(我在其中设置的popup.IsOpen="False" 被触发...
  • 你用的是什么图表中间件?
  • 这是用于 WPF 的 Delay 版本(基础的 SL 工具包图表):link,但它经过修改以支持多系列绑定(使用 Jeremiah Morrill 的 ideea + weakevents 支持以正常运行),阈值线(s)、缩放和十字准线行为....
  • 我将您的回复标记为“答案”,因为它帮助我更仔细地调查此事。我放弃了使用PopUp。我只使用Grid,它按预期工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 2018-03-09
  • 2012-09-13
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多