隐藏所有页面控件的工具提示 Silverlight

Hide tooltip for all page controls Silverlight

最近,我开始使用 Silverlight 5。我需要实现有选择地隐藏页面上所有控件的工具提示的可能性。

例如,一个页面包含一组按钮:

<Button Command="{Binding Path=VerifyDocCommand}" ToolTipService.ToolTip="{Binding Source={StaticResource Trans}, Path=ToolTipSaveButton}" CommandParameter="{Binding Path=Documents.CurrentItem, FallbackValue=null}" Style="{StaticResource VerifyButton}"/>
...

我为 ToolTip 创建了以下样式:

<navigation:Page.Resources>
    <Style TargetType="ToolTip">
        <Setter Property="Visibility" Value="{Binding ShowTooltips, Converter={StaticResource BoolToVisibilityConv}}"/>
    </Style>
</navigation:Page.Resources>

但是上面按钮的工具提示仍然可见,即使我使用这种样式:

<navigation:Page.Resources>
    <Style TargetType="ToolTip">
        <Setter Property="Visibility" Value="Collapsed"/>
    </Style>
</navigation:Page.Resources>

你能告诉我如何实现这个功能吗?

我找不到 Silverlight ToolTipService 本身提供的便捷方法。

我能想到两种可能的解决方案:

第一个解决方案:为您的项目引入编码规则,基本上说:

all ToolTip Bindings shall either use the SwitchOffConverter (if their source is the DataContext) or have a SwitchOffTextResources object as their Source

并使用如下:

<Button
    Command="{Binding VerifyDocument}"
    ToolTipService.ToolTip="{Binding Path=LocalizedText.VerifyDocument_ToolTip,
        Source={StaticResource DeactivatableUiText}}"/>

<Button
    Command="{Binding VerifyDocument}"
    ToolTipService.ToolTip="{Binding Path=VerifyDocument.Description,
        Converter={StaticResource ToolTipSwitcher}}"/>

<Resources>
    <SwitchOffUiTextResources x:Key="DeactivatableUiText"/>
    <SwitchOffConverter x:Key="ToolTipSwitcher"/>
</Resources>

public static class ToolTipSwitch
{
    private static bool s_isToolTipActivated = true;
    public static bool IsToolTipActivated
    {
        get { return s_isToolTipActivated; }
        set
        {
            if (s_isToolTipActivated != value)
            {
                s_isToolTipActivated = value;
                RaiseIsToolTipActivatedChanged();
            }
        }
    }

    private static void RaiseIsToolTipActivatedChanged()
    {
        var handlers = IsToolTipActivatedChanged;
        if (handlers != null) handlers();
    }

    public static event Action IsToolTipActivatedChanged;
}

public class SwitchOffConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ToolTipSwitch.IsToolTipActivated ? value : null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

public class SwitchOffUiTextResources : INotifyPropertyChanged
{
    public SwitchOffUiTextResources()
    {
        ToolTipSwitch.IsToolTipActivatedChanged += OnIsToolTipActivatedChanged;
    }

    private void OnIsToolTipActivatedChanged()
    {
        RaisePropertyChanged( "LocalizedText" );
    }

    private UiTextResources m_localizedText = new UiTextResources();

    public UiTextResources LocalizedText
    {
        get { return ToolTipSwitch.IsToolTipActivated ? m_localizedStrings : null; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

第二种解决方案:编写您自己的 DeactivatableToolTipService 作为 Silverlight ToolTipService 的精简包装,并且仅使用您自己的服务。如果服务被停用,只需将所有工具提示设置为空。 我完全会采用第二种方法。

<Button
    Command="{Binding VerifyDocument}"
    DeactivatableToolTipService.ToolTip="{Binding ...anything...}"/>