且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-02 08:11:52

这实际上与 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. :)