-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropdownButton.cs
More file actions
63 lines (52 loc) · 2.5 KB
/
Copy pathDropdownButton.cs
File metadata and controls
63 lines (52 loc) · 2.5 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
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
//https://github.com/givemelight/wpf-toolbar-dropdown
namespace System.Windows.Controls.Extensions {
public class DropdownButton : ToggleButton {
public DropdownButton() {
Checked += DropdownButton_Checked;
ContentTemplate = GetDataTemplate();
Style = (Style)FindResource(ToolBar.ToggleButtonStyleKey);
}
private DataTemplate GetDataTemplate() {
DataTemplate template = new DataTemplate(typeof(DropdownButton));
FrameworkElementFactory path = new FrameworkElementFactory(typeof(Path));
path.SetValue(Path.DataProperty, Geometry.Parse("F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z"));
path.SetValue(MarginProperty, new Thickness(4, 4, 0, 4));
path.SetValue(WidthProperty, 5d);
path.SetValue(Shape.FillProperty, Brushes.Black);
path.SetValue(Shape.StretchProperty, Stretch.Uniform);
path.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Right);
path.SetValue(VerticalAlignmentProperty, VerticalAlignment.Center);
FrameworkElementFactory panel = new FrameworkElementFactory(typeof(StackPanel));
panel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory contentPresenter = new FrameworkElementFactory(typeof(ContentPresenter));
Binding binding = new Binding("Content") { Source = this };
contentPresenter.SetBinding(ContentProperty, binding);
panel.AppendChild(contentPresenter);
panel.AppendChild(path);
template.VisualTree = panel;
return template;
}
private void DropdownMenu_Closed(object sender, System.Windows.RoutedEventArgs e) {
IsChecked = false;
}
private ContextMenu dropdownMenu;
public ContextMenu DropdownMenu {
get {
return dropdownMenu;
}
set {
dropdownMenu = value;
dropdownMenu.Closed += DropdownMenu_Closed;
}
}
private void DropdownButton_Checked(object sender, System.Windows.RoutedEventArgs e) {
DropdownMenu.PlacementTarget = sender as ToggleButton;
DropdownMenu.Placement = PlacementMode.Bottom;
DropdownMenu.IsOpen = true;
}
}
}