Skip to content

Instantly share code, notes, and snippets.

@punker76
Last active March 6, 2021 14:55
Show Gist options
  • Save punker76/883427640a3dd241dc6925cfda6bf0b3 to your computer and use it in GitHub Desktop.
Save punker76/883427640a3dd241dc6925cfda6bf0b3 to your computer and use it in GitHub Desktop.
A ToolTipBehavior to show different ToolTip for enabled and disabled element.
<UserControl x:Class="SampleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:utilities="clr-namespace:Utilities"
xmlns:commands="clr-namespace:Commands"
mc:Ignorable="d">
<Grid>
<Button Command="{commands:SampleCommand}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="4">
<i:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="This is a enabled Tooltip"
DisabledToolTip="Really disabled"
IsFeatureEnabled="{Binding CanUseThisFeature, Mode=OneWay}"
DisabledFeatureToolTip="This Feature is not available" />
</i:Interaction.Behaviors>
<StackPanel Orientation="Horizontal">
<iconPacks:PackIconFontAwesome Kind="SyncSolid" />
<iconPacks:PackIconFontAwesome Kind="AsteriskSolid" Margin="2 0 0 0" />
</StackPanel>
</Button>
</Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors;
namespace Utilities
{
public class ToolTipBehavior : Behavior<FrameworkElement>
{
public static readonly DependencyProperty EnabledToolTipProperty
= DependencyProperty.Register(
nameof(EnabledToolTip),
typeof(object),
typeof(ToolTipBehavior),
new PropertyMetadata(default, OnToolTipPropertyChanged));
public static readonly DependencyProperty DisabledToolTipProperty
= DependencyProperty.Register(
nameof(DisabledToolTip),
typeof(object),
typeof(ToolTipBehavior),
new PropertyMetadata(default, OnToolTipPropertyChanged));
public static readonly DependencyProperty IsFeatureEnabledProperty
= DependencyProperty.Register(
nameof(IsFeatureEnabled),
typeof(bool),
typeof(ToolTipBehavior),
new PropertyMetadata(true, OnToolTipPropertyChanged));
public static readonly DependencyProperty DisabledFeatureToolTipProperty
= DependencyProperty.Register(
nameof(DisabledFeatureToolTip),
typeof(object),
typeof(ToolTipBehavior),
new PropertyMetadata(default, OnToolTipPropertyChanged));
public object EnabledToolTip
{
get { return GetValue(EnabledToolTipProperty); }
set { SetValue(EnabledToolTipProperty, value); }
}
public object DisabledToolTip
{
get { return GetValue(DisabledToolTipProperty); }
set { SetValue(DisabledToolTipProperty, value); }
}
public bool IsFeatureEnabled
{
get { return (bool)GetValue(IsFeatureEnabledProperty); }
set { SetValue(IsFeatureEnabledProperty, value); }
}
public object DisabledFeatureToolTip
{
get { return GetValue(DisabledFeatureToolTipProperty); }
set { SetValue(DisabledFeatureToolTipProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SetCurrentValue(ToolTipService.ShowOnDisabledProperty, true);
SetTheToolTip();
AssociatedObject.IsEnabledChanged += AssociatedObject_IsEnabledChanged;
}
protected override void OnDetaching()
{
AssociatedObject.IsEnabledChanged -= AssociatedObject_IsEnabledChanged;
base.OnDetaching();
}
private static void OnToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != e.OldValue)
{
(d as ToolTipBehavior)?.SetTheToolTip();
}
}
private void AssociatedObject_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
SetTheToolTip();
}
private void SetTheToolTip()
{
if (AssociatedObject is null)
{
return;
}
if (!AssociatedObject.IsEnabled && !IsFeatureEnabled)
{
AssociatedObject.SetCurrentValue(FrameworkElement.ToolTipProperty, DisabledFeatureToolTip);
}
else if (AssociatedObject.IsEnabled)
{
AssociatedObject.SetCurrentValue(FrameworkElement.ToolTipProperty, EnabledToolTip);
}
else
{
AssociatedObject.SetCurrentValue(FrameworkElement.ToolTipProperty, DisabledToolTip);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment