forked from github/VisualStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropDownButton.cs
More file actions
89 lines (77 loc) · 2.72 KB
/
DropDownButton.cs
File metadata and controls
89 lines (77 loc) · 2.72 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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace GitHub.UI
{
public class DropDownButton : ContentControl
{
public static readonly DependencyProperty AutoCloseOnClickProperty =
DependencyProperty.Register(
"AutoCloseOnClick",
typeof(bool),
typeof(DropDownButton),
new FrameworkPropertyMetadata(true));
public static readonly DependencyProperty DropDownContentProperty =
DependencyProperty.Register(nameof(DropDownContent), typeof(object), typeof(DropDownButton));
public static readonly DependencyProperty IsOpenProperty =
Popup.IsOpenProperty.AddOwner(typeof(DropDownButton));
Button button;
Popup popup;
static DropDownButton()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(DropDownButton),
new FrameworkPropertyMetadata(typeof(DropDownButton)));
}
public bool AutoCloseOnClick
{
get { return (bool)GetValue(AutoCloseOnClickProperty); }
set { SetValue(AutoCloseOnClickProperty, value); }
}
public object DropDownContent
{
get { return GetValue(DropDownContentProperty); }
set { SetValue(DropDownContentProperty, value); }
}
public bool IsOpen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
public event EventHandler PopupOpened;
public event EventHandler PopupClosed;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
button = (Button)Template.FindName("PART_Button", this);
popup = (Popup)Template.FindName("PART_Popup", this);
button.Click += ButtonClick;
popup.Opened += OnPopupOpened;
popup.Closed += OnPopupClosed;
popup.AddHandler(MouseUpEvent, new RoutedEventHandler(PopupMouseUp), true);
}
void ButtonClick(object sender, RoutedEventArgs e)
{
IsOpen = true;
}
private void OnPopupOpened(object sender, EventArgs e)
{
IsHitTestVisible = false;
PopupOpened?.Invoke(this, e);
}
private void OnPopupClosed(object sender, EventArgs e)
{
IsOpen = false;
IsHitTestVisible = true;
PopupClosed?.Invoke(this, e);
}
private void PopupMouseUp(object sender, RoutedEventArgs e)
{
if (AutoCloseOnClick)
{
IsOpen = false;
}
}
}
}