File size: 4,764 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Microsoft.Management.UI.Internal
{
    /// <summary>
    /// Represents a toggle button used to expand or collapse elements.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]
    public partial class ExpanderButton : ToggleButton
    {
        /// <summary>
        /// Tooltip to show to expand.
        /// </summary>
        protected virtual string ExpandToolTip
        {
            get { return XamlLocalizableResources.AutoResXGen_ManagementList2_ToolTip_32; }
        }

        /// <summary>
        /// Tooltip to show to collapse.
        /// </summary>
        protected virtual string CollapseToolTip
        {
            get { return XamlLocalizableResources.CollapsingTabControl_ExpandButton_AutomationName; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpanderButton" /> class.
        /// </summary>
        public ExpanderButton()
        {
            // This constructor intentionally left blank
        }

        /// <summary>
        /// Invoked whenever the effective value of any dependency property on this <see cref="ExpanderButton"/> has been updated. The specific dependency property that changed is reported in the arguments parameter. Overrides <see cref="FrameworkElement.OnPropertyChanged"/>.
        /// </summary>
        /// <param name="e">The event data that describes the property that changed, as well as old and new values.</param>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if (e.Property == ExpanderButton.IsCheckedProperty)
            {
                this.OnIsCheckedChanged(e);
            }
        }

        /// <summary>
        /// Called when the <see cref="ToggleButton.IsChecked"/> property changes.
        /// </summary>
        /// <param name="args">The event data that describes the property that changed, as well as old and new values.</param>
        protected void OnIsCheckedChanged(DependencyPropertyChangedEventArgs args)
        {
            if (AutomationPeer.ListenerExists(AutomationEvents.PropertyChanged))
            {
                var peer = UIElementAutomationPeer.CreatePeerForElement(this);

                if (peer != null)
                {
                    var oldValue = (bool?)args.OldValue;
                    var newValue = (bool?)args.NewValue;

                    peer.RaisePropertyChangedEvent(
                        ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty,
                        (oldValue == true) ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed,
                        (newValue == true) ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed);
                }
            }

            SetToolTip();
            ToolTip toolTip = (ToolTip)this.ToolTip;
            if (toolTip.IsOpen)
            {
                // need to reset so content changes if already open
                toolTip.IsOpen = false;
                toolTip.IsOpen = true;
            }
        }

        /// <summary>
        /// Called when it has keyboard focus.
        /// </summary>
        /// <param name="args">The event data that describes getting keyboard focus.</param>
        protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs args)
        {
            SetToolTip();
            ((ToolTip)this.ToolTip).IsOpen = true;
        }

        /// <summary>
        /// Called when it lost keyboard focus.
        /// </summary>
        /// <param name="args">The event data that describes losing keyboard focus.</param>
        protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs args)
        {
            if (this.ToolTip is ToolTip toolTip)
            {
                toolTip.IsOpen = false;
            }
        }

        private void SetToolTip()
        {
            ToolTip toolTip;
            if (this.ToolTip is ToolTip)
            {
                toolTip = (ToolTip)this.ToolTip;
            }
            else
            {
                toolTip = new ToolTip();
            }

            toolTip.Content = (this.IsChecked == true) ? CollapseToolTip : ExpandToolTip;
            toolTip.PlacementTarget = this;
            toolTip.Placement = PlacementMode.Bottom;
            this.ToolTip = toolTip;
        }
    }
}