Repository navigation
Expand file tree
/
Copy pathPermissionToolTipHelper.cs
More file actions
91 lines (77 loc) · 3.55 KB
/
Copy pathPermissionToolTipHelper.cs
File metadata and controls
91 lines (77 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Windows;
using System.Windows.Controls;
namespace NETworkManager.Utilities.WPF;
/// <summary>
/// Attached-property helper to show a hint (e.g. "requires elevated rights") as tooltip on a control,
/// which is disabled because the application is missing a permission (e.g. not running as administrator).
/// The tooltip is also shown while the control is disabled and only if <see cref="HasPermissionProperty"/>
/// is <see langword="false"/>. An existing tooltip of the control is never overwritten.
/// </summary>
public static class PermissionToolTipHelper
{
/// <summary>
/// Resource key of the tooltip style (limits the width and wraps long text).
/// Defined in "Resources/Styles/ToolTipStyles.xaml" of the application.
/// </summary>
private const string ToolTipStyleKey = "WrapToolTip";
/// <summary>
/// Text of the tooltip, which is shown if the permission is missing.
/// </summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.RegisterAttached(
"Text",
typeof(string),
typeof(PermissionToolTipHelper),
new PropertyMetadata(null, OnChanged));
public static void SetText(FrameworkElement element, string value) =>
element.SetValue(TextProperty, value);
public static string GetText(FrameworkElement element) =>
(string)element.GetValue(TextProperty);
/// <summary>
/// Indicates whether the permission is available (e.g. running as administrator).
/// Defaults to <see langword="true"/>, so no tooltip is shown until the binding is resolved.
/// </summary>
public static readonly DependencyProperty HasPermissionProperty =
DependencyProperty.RegisterAttached(
"HasPermission",
typeof(bool),
typeof(PermissionToolTipHelper),
new PropertyMetadata(true, OnChanged));
public static void SetHasPermission(FrameworkElement element, bool value) =>
element.SetValue(HasPermissionProperty, value);
public static bool GetHasPermission(FrameworkElement element) =>
(bool)element.GetValue(HasPermissionProperty);
/// <summary>
/// Indicates whether the tooltip was set by this helper (to only clear our own tooltip).
/// </summary>
private static readonly DependencyProperty IsAppliedProperty =
DependencyProperty.RegisterAttached(
"IsApplied",
typeof(bool),
typeof(PermissionToolTipHelper),
new PropertyMetadata(false));
private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not FrameworkElement element)
return;
var text = GetText(element);
var isApplied = (bool)element.GetValue(IsAppliedProperty);
if (!GetHasPermission(element) && !string.IsNullOrEmpty(text))
{
// Don't overwrite a tooltip that was set on the control itself
if (!isApplied && element.ToolTip != null)
return;
var toolTip = new ToolTip { Content = text };
toolTip.SetResourceReference(FrameworkElement.StyleProperty, ToolTipStyleKey);
element.ToolTip = toolTip;
ToolTipService.SetShowOnDisabled(element, true);
element.SetValue(IsAppliedProperty, true);
}
else if (isApplied)
{
element.ClearValue(FrameworkElement.ToolTipProperty);
element.ClearValue(ToolTipService.ShowOnDisabledProperty);
element.SetValue(IsAppliedProperty, false);
}
}
}