Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 启用/禁用wpf应用程序中所有控件的工具提示_C#_Wpf_Tooltip - Fatal编程技术网

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

C# 启用/禁用wpf应用程序中所有控件的工具提示,c#,wpf,tooltip,C#,Wpf,Tooltip,我正在编写一个WPF应用程序,它有许多不同的控件,每个控件都有自己的工具提示。虽然工具提示很有用,但其中一些提示很长,会造成阻碍 我想能够创建一个按钮,将启用和禁用应用程序上的所有工具提示时,点击它。我一直在做的似乎是一个非常漫长和不必要的方法。有什么方法可以快速完成我想要的吗?您可以尝试添加一种隐式样式,将所有顶级窗口的工具提示的可见性属性设置为折叠的。大概是这样的: private bool _isToolTipVisible = true; private void Button_Clic

我正在编写一个WPF应用程序,它有许多不同的控件,每个控件都有自己的工具提示。虽然工具提示很有用,但其中一些提示很长,会造成阻碍


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

您可以尝试添加一种隐式样式,将所有顶级窗口的
工具提示的
可见性属性设置为
折叠的
。大概是这样的:

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>

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

此代码允许您定制提示,并在应用程序范围内轻松打开和关闭它们。在我的例子中,我将在用户设置中保存工具提示可见性。该设置应用于主窗口加载,然后在设置更改时更新

在App.xaml中:


当我禁用工具提示时,这会起作用,但当我尝试在第二次单击按钮时重新启用工具提示时,会出现一个错误,显示已添加
项。在字典中输入:“system.windows.controls.tooltips”正在添加的键:“system.windows.controls.tooltips”
错误发生在上述代码的这一行
window.Resources.Add(typeof(ToolTip),style)//隐藏
Ensbles不会添加样式。它会移除它。您需要按照我的示例设置_isToolTipVisible。我去掉了代码中的
foreach
循环,然后将
this
放在
窗口的位置,我可以切换工具提示。谢谢!!
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);

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