Silverlight DescriptionViewer 防止工具提示在单击时消失

发布于 2024-11-04 05:24:58 字数 709 浏览 5 评论 0原文

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

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

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

不幸的是,某些用户单击显示的图标(因为它甚至默认情况下悬停),这会立即关闭工具提示(实际且唯一的信息)。更糟糕的是,当您悬停后快速单击时,工具提示甚至根本不会出现,因此用户可能会认为图标根本没有任何功能。

我想防止在单击时关闭工具提示(只需在“鼠标移开”时关闭),更好的单击应该强制工具提示立即显示(在显示之前跳过通常的超时)。

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

你能帮忙吗?谢谢你!

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

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." />

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).

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.

Can you help? Thank you!

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

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

发布评论

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

评论(1

断爱 2024-11-11 05:24:58

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

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

更新:

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

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

公共静态类 ButtonAttachedProperties
{
公共静态 bool GetOpenToolTip(DependencyObject obj)
{
返回 (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 找到它。

                        <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>

希望这有帮助。 :)

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.

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.

Update:

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.

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;
    };
}

}

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. :)

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