【问题标题】:Tooltip Initial delay not working in WPF?工具提示初始延迟在 WPF 中不起作用?
【发布时间】:2023-04-11 02:55:01
【问题描述】:

我在 WPF 中创建了四个按钮。我为每个按钮使用工具提示。我给 Tooltip 一个初始延迟值。当我第一次将鼠标悬停到这些按钮中的任何一个时,初始延迟有效,但是当我将一个按钮移动到另一个按钮时,不会触发初始延迟。当我将鼠标光标从一个按钮控件移动到另一个按钮控件时,我想禁用工具提示。但不幸的是,它不起作用。

<Button
    Style="{StaticResource RoundCorner}"
    Name="button1"
    Width="71"
    HorizontalAlignment="Left"
    Margin="381,14,0,0"
    Height="24"
    VerticalAlignment="Top"
    UseLayoutRounding="True"
    RenderOptions.ClearTypeHint="Enabled"
    RenderOptions.BitmapScalingMode="NearestNeighbor"
    SnapsToDevicePixels="True"
    ToolTipService.InitialShowDelay="1000"
    ToolTipService.BetweenShowDelay="0"
    ToolTipService.ShowDuration="7000">
    <Button.Content>
        <TextBlock FontSize="10" FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display">
            Help
        </TextBlock>
    </Button.Content>
    <Button.ToolTip>
        <ToolTip  UseLayoutRounding="True"  RenderOptions.ClearTypeHint="Enabled"  RenderOptions.BitmapScalingMode="NearestNeighbor"   SnapsToDevicePixels="True" TextOptions.TextFormattingMode="Display">
            <StackPanel>
                <TextBlock  FontFamily="Segoe UI"  FontSize="12"  TextOptions.TextFormattingMode="Ideal"    >
                    Help
                </TextBlock>
            </StackPanel>
        </ToolTip>
    </Button.ToolTip>
</Button>

【问题讨论】:

  • 您可以使用 ToolTipService.IsEnabled ="{Binding ElementName=button1, Path=IsFocused}" 禁用 button1 的工具提示。当 button1 的 Tooltip 被禁用时它没有重点。
  • @DasiyTianMSFT 您的代码不起作用,我尝试了您的数据绑定,但不幸的是,使用您的代码后没有出现 ToolTip,请提供其他解决方案。

标签: wpf xaml button tooltip delay


【解决方案1】:

问题在于光标在具有工具提示的元素之间移动所花费的时间。如果没有时间或时间很短,则忽略 InitialShowDelay。这本质上是因为 tooltipservice 没有关闭第一个 tooltip,它仍然有一个 tooltip 打开。它只是拾取下一个按钮的工具提示内容。

简单的解决方案是在按钮之间留出空隙。

这可以确保在鼠标悬停在另一个带有工具提示的元素上之前,之前的工具提示已经关闭。

因此,如果您尝试以下操作:

    <StackPanel>
        <Button Content="Apple"
                ToolTipService.InitialShowDelay="1000"   
                ToolTipService.BetweenShowDelay="2"
                ToolTip="An Apple"
                Margin="5"
                />
        <Button Content="Banana"
                ToolTip="A Banana"
                ToolTipService.InitialShowDelay="1000"   
                ToolTipService.BetweenShowDelay="2"
                Margin="5"
                />
        <Button Content="Carrot"
                ToolTip="Orange Carrot"
                ToolTipService.InitialShowDelay="1000"   
                ToolTipService.BetweenShowDelay="2"
                Margin="5"
                />
    </StackPanel>

按钮之间有间隙,如果将光标从第一个按钮移动到第二个按钮,则会有延迟。

在图片中,请注意按钮之间有一个间隙。

如果您删除我在所有按钮上的边距,则不会有间隙,并且当您将鼠标从第一个按钮移动到第二个按钮时,第二个工具提示将立即显示。

如果你只能有一个很小的差距,那么一个相当复杂的解决方案是必要的。您需要在鼠标离开其中一个按钮时立即清空或关闭工具提示。

一种方法是使用触发器。 触发器在满足其逻辑时设置属性,因此使用样式将属性设置为 null,以便在触发器为 false 时为 null。 当鼠标悬停在按钮上时,将工具提示设置为一个值。 因此,当鼠标移到按钮上时,按钮只有一个工具提示,当鼠标离开按钮时,我们的第一个工具提示将立即为空。

您需要在某处放置工具提示标记,并且可以添加附加属性,但以下解决方案使用按钮标记。

最小或没有差距:

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="ToolTip" Value="{x:Null}" />
        <Setter Property="ToolTipService.InitialShowDelay" Value="1000"/>
        <Setter Property="ToolTipService.BetweenShowDelay" Value="0"/>
        <Setter Property="Margin" Value="1"/>
        <Style.Triggers>
            <Trigger  Property="IsMouseOver" Value="True">
                <Setter Property="ToolTip" Value="{Binding Tag, RelativeSource={RelativeSource Self}}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
   <StackPanel>
        <Button Content="Apple">
            <Button.Tag>
                <TextBlock Text="This textBlock illustrates a more sophisticated tag" FontFamily="Segoe UI"  FontSize="12"/>
            </Button.Tag>
        </Button>

        <Button Content="Banana"
                Tag="A Banana"
                />
        <Button Content="Carrot"
                Tag="Orange Carrot"
                />
    </StackPanel>
</Grid>

请注意,第一个按钮使用点表示法来说明如何将您喜欢的任何标记放入标签中,而不仅仅是第二个和第三个按钮使用的字符串。

如果您希望一种样式包含其他样式的设置,您可以使用 BasedOn:

    <Style TargetType="{x:Type Button}" x:Key="RoundCorner">
        .....
    </Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource RoundCorner}">

此样式没有键,因此它将应用于其范围内的所有按钮。如果您不希望将样式应用于某些按钮,则可以为样式指定一个键。

【讨论】:

  • 其实我的代码没有问题,我只发布了一个按钮代码,我所有的四个按钮代码都是一样的,所有按钮控件都有适当的边距并用适当的标签成功关闭,但它仍然是不工作..请帮忙。
  • @MERUNKUMARMAITY 您的代码是正确的,我刚刚尝试过。当按钮彼此相邻时,工具提示延迟就会消失。您必须在每个按钮之间添加空格。
  • 我有四个按钮,每个按钮都被适当的标签关闭,那么为什么你看到我必须在它们之间添加空格?你是什​​么意思。我知道添加适当的空间意味着用适当的标签关闭按钮。如果不是,您能否详细解释一下,我无法理解“添加空间”一词。
  • 设置按钮的边距值,使它们彼此保持距离。
  • 是的,不是结束标签是你的问题。这是因为渲染时按钮之间没有空格。如果光标移动并且在移动时它直接从一个按钮移动到另一个按钮,那么您将不会有任何延迟。这是因为没有时间工具提示是不合适的。我在上面添加了一张图片。箭头指向一个间隙。如果您没有设置 MARGIN,那么就不会有差距。您可以使用除边距以外的其他东西......只要在渲染时按钮之间存在间隙。
【解决方案2】:

我不知道 ToolTipService 有什么问题,但唯一对我有用的是将 BetweenShowDelay 设置为一个小整数值,然后我正在寻找的行为起作用。我知道文档说 BetweenShowDelay 应该以毫秒为单位,但是当我尝试使用 ms 值时,工具提示将立即显示,因为我在 InitialShowDelay 之后将鼠标悬停到下一个控件上,因为一个控件已过去。我什至检查了 .NET 参考源,但找不到代码问题 (https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/PopupControlService.cs,dd8bf024ff5d4d74)

这对我有用:

<StackPanel>    
    <Button ToolTipService.InitialShowDelay="2000"
            ToolTipService.BetweenShowDelay="1"
            ToolTip="Say something" />

    <Button ToolTipService.InitialShowDelay="2000"
            ToolTipService.BetweenShowDelay="1"
            ToolTip="Say something else" />
</StackPanel>

“说点什么”工具提示显示后,如果我将鼠标悬停在下一个按钮上,则在初始延迟 2000 毫秒过去之前,工具提示不会显示。这不是文档和参考源所说的它应该如何工作,但这是我让它为我工作以获得预期行为的方式。

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多