forked from givemelight/wpf-toolbar-dropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropdownButton.cs
70 lines (60 loc) · 2.51 KB
/
DropdownButton.cs
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
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
namespace System.Windows.Controls.Extensions
{
public class DropdownButton : ToggleButton
{
public DropdownButton()
{
Checked += DropdownButton_Checked;
ContentTemplate = GetDataTemplate();
Style = (Style)FindResource(ToolBar.ToggleButtonStyleKey);
}
private DataTemplate GetDataTemplate()
{
var template = new DataTemplate(typeof(DropdownButton));
var 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);
var panel = new FrameworkElementFactory(typeof(StackPanel));
panel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
var contentPresenter = new FrameworkElementFactory(typeof(ContentPresenter));
var 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;
}
}
}