Enable/Disable wpf 应用程序中所有控件的工具提示

Enable/Disable ToolTips for all controls in wpf app

我正在编写一个 WPF 应用程序,它有很多不同的控件,每个控件都有自己的 ToolTip。虽然 ToolTips 很有用,但有些太长了,很碍事。

我希望能够创建一个按钮,该按钮将在单击时启用和禁用应用程序上的所有工具提示。我一直在做的事情似乎是一种非常漫长且不必要的方法。有什么方法可以快速完成我想要的吗?

您可以尝试添加隐式样式,将所有 ToolTipsVisbility 属性 设置为 top-level windows 的 Collapsed ].像这样:

private bool _isToolTipVisible = true;
private void Button_Click(object sender, RoutedEventArgs e)
{
    Style style = new Style(typeof(ToolTip));
    style.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
    style.Seal();

    foreach (Window window in Application.Current.Windows)
    {
        if (_isToolTipVisible)
        {
            window.Resources.Add(typeof(ToolTip), style); //hide
            _isToolTipVisible = false;
        }
        else
        {
            window.Resources.Remove(typeof(ToolTip)); //show
            _isToolTipVisible = true;
        }
    }
}

您可以将全局工具提示样式添加到应用程序资源中:

<Application.Resources>
    <Style TargetType="{x:Type ToolTip}">
        <Setter Property="Template" Value="{x:Null}" />
    </Style>
</Application.Resources>

这将禁用所有工具提示。

要切换使用以下内容:

Style _style;

void Button_Click(object sender, RoutedEventArgs e)
{
    if (_style == null)
    {
        _style = (Style)Application.Current.Resources[typeof(ToolTip)];
        Application.Current.Resources.Remove(typeof(ToolTip));
    }
    else
        Application.Current.Resources.Add(typeof(ToolTip), _style);

}

我需要可以关闭的自定义工具提示。其他解决方案并没有完全涵盖这一点,所以这就是我最终做的......

此代码允许您拥有自定义提示,并在整个应用程序范围内轻松打开和关闭它们。就我而言,我将工具提示可见性保存在用户设置中。该设置应用于主 window 负载,然后在设置更改时更新。

App.xaml:

<Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border Name="Border" BorderThickness="1" Background="PaleGoldenrod" CornerRadius="4" BorderBrush="Black">
                    <ContentPresenter Margin="4"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    ... various other settings
</Style>

<Style x:Key="NoTip" TargetType="ToolTip">
    <Setter Property="Visibility" Value="Collapsed"/>
</Style>

然后,在我的 MainWindow.xaml.cs 中(但可以去任何你改变提示可见性的地方):

private Style styleWithTips;
private Style styleNoTips;
...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Set initial tips on/off
    styleWithTips = (Style)Application.Current.Resources[typeof(ToolTip)];
    styleNoTips = (Style)Application.Current.Resources["NoTip"];
    updateTipStyle();
}

private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "ShowHints")
    {
        updateTipStyle();
    }
}
...
private void updateTipStyle()
{
    ResourceDictionary resources = Application.Current.Resources;
    bool showHints = Properties.Settings.Default.ShowHints;
    if (resources.Contains(typeof(ToolTip)))
    {
        resources.Remove(typeof(ToolTip));
    }
    resources.Add(typeof(ToolTip), showHints ? styleWithTips: styleNoTips);
}