Silverlight DescriptionViewer 防止工具提示在点击时消失 [英] Silverlight DescriptionViewer prevent Tooltip to disappear on click

查看:31
本文介绍了Silverlight DescriptionViewer 防止工具提示在点击时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Silverlight 4 DescriptionViewer 控件在工具提示中显示一个 Description:

The Silverlight 4 DescriptionViewer Control displays a Description in a Tooltip:

语法非常简单:将命名空间 xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 和以下 XAML 添加到您的用户控件:

The syntax is pretty easy: Add Namespace xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" and the following XAML to your UserControl:

<dataInput:DescriptionViewer Description="Some hints on user input etc." />

不幸的是,一些用户点击显示的图标(因为它甚至默认悬停)立即关闭工具提示(实际和唯一的信息).更糟糕的是,当你悬停后快速点击时,Tooltip甚至根本不会出现,所以用户可能会认为Icon根本没有任何功能.

Unfortunately, some users click on the displayed Icon (since it even hovers by default) which instantly closes the Tooltip (the actual and only information). Even worse, when you click very quickly after hovering, the Tooltip even won't appear at all, so the user might think the Icon has no function at all.

我想防止在点击时关闭工具提示(只需在鼠标移出"时关闭),更好的点击应该会强制工具提示立即显示(在显示之前跳过通常的超时).

I'd like to prevent closing the Tooltip on Click (just close on "mouse out"), even better clicking should force the Tooltip to show immediately (skipping the usual timeout before shown).

这似乎比我更难,因为没有 OnClick 事件,而且 MouseLeftButtonDown 似乎根本没有触发.我还尝试覆盖 DescriptionViewer 控件,但也没有找到合适的方法来覆盖.

It seems to be harder then I though, since there is no OnClick event and also MouseLeftButtonDown seems to not fire at all. I also tried to override the DescriptionViewer Control but also no luck finding the appropriate methods to override.

你能帮忙吗?谢谢!

推荐答案

这实际上与 DescriptionViewer 无关,它是 ToolTip 的行为.单击鼠标后,工具提示将消失.在这种情况下,您可能想要编写自己的工具提示.

this actually has nothing to do with the DescriptionViewer, it is the behavior of the ToolTip. The ToolTip will disappear once you do a mouse click. In this case you might want to write you own ToolTip.

我认为通常当您单击 DescriptionViewer 图标时,应该会打开一个新窗口,该窗口类似于更详细的帮助页面.这样用户就不会感到困惑.

I think normally when you click on the DescriptionViewer icon, there should be a new window open which is like a more detailed help page. So the user wouldn't get confused.

更新:

您可以通过定义附加属性来实现这一点.基本上,您将此属性附加到您的 DescriptionViewer 中的 Button.当 Button 的 Click 事件被触发时,您会在 Button 下方找到 Tooltip 并将其 IsOpen 设置为 ture.然后,您还需要处理 MouseLeave 事件以在鼠标离开后隐藏工具提示.

You can achieve this by defining an attached property. Basically, you attach this property to the Button inside your DescriptionViewer. When the Button's Click event is fired, you find the Tooltip underneath the Button and set its IsOpen to ture. Then you also need to handle the MouseLeave event to hide the Tooltip once your mouse's away.

这就是附加属性的定义方式.

This is how the attached property is defined.

公共静态类ButtonAttachedProperties{public static bool GetOpenToolTip(DependencyObject obj){返回(布尔)obj.GetValue(OpenToolTipProperty);}

public static class ButtonAttachedProperties { public static bool GetOpenToolTip(DependencyObject obj) { return (bool)obj.GetValue(OpenToolTipProperty); }

public static void SetOpenToolTip(DependencyObject obj, bool value)
{
    obj.SetValue(OpenToolTipProperty, value);
}

public static readonly DependencyProperty OpenToolTipProperty =
    DependencyProperty.RegisterAttached("OpenToolTip", typeof(bool), typeof(ButtonAttachedProperties), new PropertyMetadata(false, Callback));


private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var button = d as Button;

    if (button == null || !(bool)e.NewValue) return;

    button.Click += (s, e1) =>
    {
        var tooltip = button.FindName("MyToolTip") as ToolTip;

        if (tooltip != null)
        {
            tooltip.PlacementTarget = button;
            tooltip.IsOpen = true;      
        }
    };

    button.MouseLeave += (s, e2) =>
    {
        var tooltip = button.FindName("MyToolTip") as ToolTip;

        if (tooltip != null)
            tooltip.IsOpen = false;
    };
}

}

然后在 DescriptionViewer 的样式中,将属性附加到 Button.您还需要命名工具提示,以便能够在附加属性类中使用 FindName 找到它.

Then in the DescriptionViewer's style, you attach the property to the Button. Also you need to name the Tooltip in order to be able to find it using FindName in the attach property class.

                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Padding="{TemplateBinding Padding}" Width="{TemplateBinding Width}">
                            <Button x:Name="DescriptionContent" local:ButtonAttachedProperties.OpenToolTip="True" BorderBrush="#FFFFFFFF" BorderThickness="1" Background="#00000000" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="False" Padding="1" Template="{TemplateBinding GlyphTemplate}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                <ToolTipService.ToolTip>
                                    <ToolTip x:Name="MyToolTip" Content="{TemplateBinding Description}" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Style="{TemplateBinding ToolTipStyle}"/>
                                </ToolTipService.ToolTip>
                            </Button>
                        </Border>

希望这会有所帮助.:)

Hope this helps. :)

这篇关于Silverlight DescriptionViewer 防止工具提示在点击时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆