I am writing a WPF application which has a lot of different controls, each with its own ToolTip. Although the ToolTips are useful, some of them are quite long and get in the way.
I want to be able to create a button which will enable and disable all the tooltips on the app when it is clicked. what I've been working on seems like a really long and unnecessary way of going at is. Is there a way to accomplish what I want in a quick manner?
You could try to add an implicit style that sets the Visbility property of all ToolTips to Collapsed for all top-level windows. Something like this:
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;
}
}
}
You can add global tooltip style into application resources:
<Application.Resources>
<Style TargetType="{x:Type ToolTip}">
<Setter Property="Template" Value="{x:Null}" />
</Style>
</Application.Resources>
This disable all tooltips.
To toggle use following:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With