blob: 4654d7ba283040c385e7fc37917df058c36d9aad (
plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MenuItemExtensions.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// Defines the MenuItemExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.AttachedProperties
{
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
/// <summary>
/// The menu item extensions.
/// </summary>
public class MenuItemExtensions : DependencyObject
{
#region Constants and Fields
/// <summary>
/// The group name property.
/// </summary>
public static readonly DependencyProperty GroupNameProperty = DependencyProperty.RegisterAttached(
"GroupName",
typeof(string),
typeof(MenuItemExtensions),
new PropertyMetadata(string.Empty, OnGroupNameChanged));
/// <summary>
/// The element to group names.
/// </summary>
public static Dictionary<MenuItem, string> ElementToGroupNames = new Dictionary<MenuItem, string>();
#endregion
#region Public Methods
/// <summary>
/// The get group name.
/// </summary>
/// <param name="element">
/// The element.
/// </param>
/// <returns>
/// The group name as a string.
/// </returns>
public static string GetGroupName(MenuItem element)
{
return element.GetValue(GroupNameProperty).ToString();
}
/// <summary>
/// The set group name.
/// </summary>
/// <param name="element">
/// The element.
/// </param>
/// <param name="value">
/// The value.
/// </param>
public static void SetGroupName(MenuItem element, string value)
{
element.SetValue(GroupNameProperty, value);
}
#endregion
#region Methods
/// <summary>
/// The menu item checked.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private static void MenuItemChecked(object sender, RoutedEventArgs e)
{
var menuItem = e.OriginalSource as MenuItem;
foreach (var item in ElementToGroupNames)
{
if (item.Key != menuItem && item.Value == GetGroupName(menuItem))
{
item.Key.IsChecked = false;
}
}
}
/// <summary>
/// The on group name changed.
/// </summary>
/// <param name="d">
/// The d.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MenuItem menuItem = d as MenuItem;
if (menuItem != null)
{
string newGroupName = e.NewValue.ToString();
string oldGroupName = e.OldValue.ToString();
if (string.IsNullOrEmpty(newGroupName))
{
RemoveCheckboxFromGrouping(menuItem);
}
else
{
if (newGroupName != oldGroupName)
{
if (!string.IsNullOrEmpty(oldGroupName))
{
RemoveCheckboxFromGrouping(menuItem);
}
ElementToGroupNames.Add(menuItem, e.NewValue.ToString());
menuItem.Checked += MenuItemChecked;
}
}
}
}
/// <summary>
/// The remove checkbox from grouping.
/// </summary>
/// <param name="checkBox">
/// The check box.
/// </param>
private static void RemoveCheckboxFromGrouping(MenuItem checkBox)
{
ElementToGroupNames.Remove(checkBox);
checkBox.Checked -= MenuItemChecked;
}
#endregion
}
}
|