Skip to content

Commit 7f7826a

Browse files
committed
Replaced derived MenuItem classes with keyed styles.
1 parent 58f2169 commit 7f7826a

File tree

7 files changed

+59
-58
lines changed

7 files changed

+59
-58
lines changed

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,7 @@
329329
<Compile Include="SnackbarMessageEventArgs.cs" />
330330
<Compile Include="SnackbarMessageQueue.cs" />
331331
<Compile Include="SnackbarMessageQueueItem.cs" />
332-
<Compile Include="SpellingIgnoreAllMenuItem.cs" />
333-
<Compile Include="SpellingNoSuggestionsMenuItem.cs" />
334-
<Compile Include="SpellingSuggestionMenuItem.cs" />
332+
<Compile Include="Spelling.cs" />
335333
<Compile Include="Transitions\CircleWipe.cs" />
336334
<Compile Include="IHintProxy.cs" />
337335
<Compile Include="Transitions\IndexedItemOffsetMultiplierExtension.cs" />

MaterialDesignThemes.Wpf/Spelling.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Windows;
2+
3+
namespace MaterialDesignThemes.Wpf
4+
{
5+
public static class Spelling
6+
{
7+
public static ResourceKey SpellingSuggestionMenuItemStyleKey { get; } = new ComponentResourceKey(typeof(Spelling), ResourceKeyId.SpellingSuggestionMenuItemStyle);
8+
public static ResourceKey SpellingIgnoreAllMenuItemStyleKey { get; } = new ComponentResourceKey(typeof(Spelling), ResourceKeyId.SpellingIgnoreAllMenuItemStyle);
9+
public static ResourceKey SpellingNoSuggestionsMenuItemStyleKey { get; } = new ComponentResourceKey(typeof(Spelling), ResourceKeyId.SpellingNoSuggestionsMenuItemStyle);
10+
public static ResourceKey SpellingSeparatorStyleKey { get; } = new ComponentResourceKey(typeof(Spelling), ResourceKeyId.SpellingSeparatorStyle);
11+
}
12+
13+
internal enum ResourceKeyId
14+
{
15+
SpellingSuggestionMenuItemStyle,
16+
SpellingIgnoreAllMenuItemStyle,
17+
SpellingNoSuggestionsMenuItemStyle,
18+
SpellingSeparatorStyle,
19+
}
20+
}

MaterialDesignThemes.Wpf/SpellingIgnoreAllMenuItem.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

MaterialDesignThemes.Wpf/SpellingNoSuggestionsMenuItem.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

MaterialDesignThemes.Wpf/SpellingSuggestionMenuItem.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

MaterialDesignThemes.Wpf/TextFieldAssist.cs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Windows;
33
using System.Windows.Controls;
44
using System.Windows.Controls.Primitives;
5+
using System.Windows.Documents;
56

67
namespace MaterialDesignThemes.Wpf
78
{
@@ -46,7 +47,7 @@ public static Thickness GetTextBoxViewMargin(DependencyObject element)
4647
/// Controls the visibility of the underline decoration.
4748
/// </summary>
4849
public static readonly DependencyProperty DecorationVisibilityProperty = DependencyProperty.RegisterAttached(
49-
"DecorationVisibility", typeof (Visibility), typeof (TextFieldAssist), new PropertyMetadata(default(Visibility)));
50+
"DecorationVisibility", typeof(Visibility), typeof(TextFieldAssist), new PropertyMetadata(default(Visibility)));
5051

5152
/// <summary>
5253
/// Controls the visibility of the underline decoration.
@@ -106,38 +107,60 @@ private static void TextBoxOnContextMenuOpening(object sender, ContextMenuEventA
106107

107108
ContextMenu contextMenu = textBoxBase?.ContextMenu;
108109
if (contextMenu == null) return;
109-
110+
110111
RemoveSpellingSuggestions(contextMenu);
111112

112113
if (!SpellCheck.GetIsEnabled(textBoxBase)) return;
113114

114115
SpellingError spellingError = GetSpellingError(textBoxBase);
115116
if (spellingError != null)
116117
{
118+
Style spellingSuggestionStyle =
119+
contextMenu.TryFindResource(Spelling.SpellingSuggestionMenuItemStyleKey) as Style;
120+
117121
int insertionIndex = 0;
118122
bool hasSuggestion = false;
119123
foreach (string suggestion in spellingError.Suggestions)
120124
{
121125
hasSuggestion = true;
122-
var menuItem = new SpellingSuggestionMenuItem(suggestion)
126+
var menuItem = new MenuItem
123127
{
124-
CommandTarget = textBoxBase
128+
CommandTarget = textBoxBase,
129+
Command = EditingCommands.CorrectSpellingError,
130+
CommandParameter = suggestion,
131+
Style = spellingSuggestionStyle,
132+
Tag = typeof(Spelling)
125133
};
126134
contextMenu.Items.Insert(insertionIndex++, menuItem);
127135
}
128136
if (!hasSuggestion)
129137
{
130-
contextMenu.Items.Insert(insertionIndex++, new SpellingNoSuggestionsMenuItem());
138+
contextMenu.Items.Insert(insertionIndex++, new MenuItem
139+
{
140+
Style = contextMenu.TryFindResource(Spelling.SpellingNoSuggestionsMenuItemStyleKey) as Style,
141+
Tag = typeof(Spelling)
142+
});
131143
}
132144

133-
contextMenu.Items.Insert(insertionIndex++, new Separator { Tag = typeof(SpellingSuggestionMenuItem) });
145+
contextMenu.Items.Insert(insertionIndex++, new Separator
146+
{
147+
Style = contextMenu.TryFindResource(Spelling.SpellingSeparatorStyleKey) as Style,
148+
Tag = typeof(Spelling)
149+
});
134150

135-
contextMenu.Items.Insert(insertionIndex++, new SpellingIgnoreAllMenuItem
151+
contextMenu.Items.Insert(insertionIndex++, new MenuItem
136152
{
137-
CommandTarget = textBoxBase
153+
Command = EditingCommands.IgnoreSpellingError,
154+
CommandTarget = textBoxBase,
155+
Style = contextMenu.TryFindResource(Spelling.SpellingIgnoreAllMenuItemStyleKey) as Style,
156+
Tag = typeof(Spelling)
138157
});
139158

140-
contextMenu.Items.Insert(insertionIndex, new Separator { Tag = typeof(SpellingSuggestionMenuItem) });
159+
contextMenu.Items.Insert(insertionIndex, new Separator
160+
{
161+
Style = contextMenu.TryFindResource(Spelling.SpellingSeparatorStyleKey) as Style,
162+
Tag = typeof(Spelling)
163+
});
141164
}
142165
}
143166

@@ -167,12 +190,8 @@ private static void TextBoxOnContextMenuClosing(object sender, ContextMenuEventA
167190

168191
private static void RemoveSpellingSuggestions(ContextMenu menu)
169192
{
170-
foreach (object item in (from item in menu.Items.OfType<object>()
171-
let separator = item as Separator
172-
where item is SpellingSuggestionMenuItem ||
173-
item is SpellingIgnoreAllMenuItem ||
174-
item is SpellingNoSuggestionsMenuItem ||
175-
ReferenceEquals(separator?.Tag, typeof(SpellingSuggestionMenuItem))
193+
foreach (FrameworkElement item in (from item in menu.Items.OfType<FrameworkElement>()
194+
where ReferenceEquals(item.Tag, typeof(Spelling))
176195
select item).ToList())
177196
{
178197
menu.Items.Remove(item);

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Menu.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,16 @@
275275
</Setter>
276276
</Style>
277277

278-
<Style TargetType="{x:Type wpf:SpellingSuggestionMenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
278+
<Style x:Key="{x:Static wpf:Spelling.SpellingSuggestionMenuItemStyleKey}" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
279279
<Setter Property="FontWeight" Value="Bold" />
280-
<Setter Property="Header" Value="{Binding RelativeSource={RelativeSource Self}, Path=Suggestion}" />
280+
<Setter Property="Header" Value="{Binding RelativeSource={RelativeSource Self}, Path=CommandParameter}" />
281281
</Style>
282282

283-
<Style TargetType="{x:Type wpf:SpellingIgnoreAllMenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
283+
<Style x:Key="{x:Static wpf:Spelling.SpellingIgnoreAllMenuItemStyleKey}" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
284284
<Setter Property="Header" Value="Ignore All" />
285285
</Style>
286286

287-
<Style TargetType="{x:Type wpf:SpellingNoSuggestionsMenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
287+
<Style x:Key="{x:Static wpf:Spelling.SpellingNoSuggestionsMenuItemStyleKey}" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
288288
<Setter Property="Header" Value="(no spelling suggestions)" />
289289
<Setter Property="IsEnabled" Value="False" />
290290
</Style>

0 commit comments

Comments
 (0)