启用/禁用WPF应用程序中所有控件的工具提示

3
我正在编写一个WPF应用程序,其中包含许多不同的控件,每个控件都有自己的工具提示。虽然这些工具提示很有用,但其中一些相当长,会妨碍视线。
我想创建一个按钮,当单击该按钮时,可以启用和禁用应用程序中的所有工具提示。我一直在努力寻找一种看起来非常冗长和不必要的方法。是否有一种快速完成我想要的功能的方法?
3个回答

5
你可以尝试添加一个隐式样式,将所有顶层窗口的ToolTipVisbility属性设置为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;
        }
    }
}

当我禁用工具提示时,这是有效的,但是当我尝试在第二个按钮单击上重新启用它们时,会出现一个错误,指出“项已被添加。字典中的键:'system.windows.controls.tooltips'正在添加的键:'system.windows.controls.tooltips'”。 - Stavros
以上代码中,错误发生在此行 window.Resources.Add(typeof(ToolTip), style); //hide - Stavros
合并它们不会添加样式,而是删除它。您需要根据我的示例设置_isToolTipVisible。 - mm8
我在代码中摆脱了foreach循环,然后将window替换为this,这样我就能够切换工具提示了。非常感谢!! - Stavros

1
你可以将全局的工具提示样式添加到应用程序资源中:
<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);

}

1
我需要有自定义工具提示,可以关闭。其他的解决方案并不完全覆盖这个问题,所以这是我最终采取的措施...
这段代码允许您拥有自定义提示,并轻松地在整个应用程序中打开和关闭它们。在我的情况下,我将工具提示可见性保存在用户设置中。该设置在主窗口加载时应用,如果更改了设置,则会进行更新。
在中:
<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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接