Skip to content

Commit a013506

Browse files
BornToBeRootclaude
andcommitted
Chore: Menu items: Content alignment without binding
MahApps binds HorizontalContentAlignment / VerticalContentAlignment of a MenuItem to its parent ItemsControl. Menu items created outside of a menu (CompositeCollection of the profile context menu, x:Array with the extra items of the host views) have no parent when the style is applied, the binding fails and is logged as XAML binding error. New DefaultMenuItem style (implicit) with the values of the context menus (Left / Center), keyed MenuItem styles are based on it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7771090 commit a013506

5 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎Source/NETworkManager/Resources/Styles/MenuItemStyles.xaml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks">
4+
<!--
5+
MahApps binds HorizontalContentAlignment / VerticalContentAlignment of a MenuItem to its parent ItemsControl
6+
(RelativeSource FindAncestor). A MenuItem created outside of a menu (e.g. in a CompositeCollection or an x:Array
7+
resource with extra items of the profile context menu) has no parent yet, the binding fails and is logged as XAML
8+
binding error. Context menus use Left / Center (MahApps.Styles.ContextMenu), so the values are set directly.
9+
-->
10+
<Style x:Key="DefaultMenuItem" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
11+
<Setter Property="HorizontalContentAlignment" Value="Left" />
12+
<Setter Property="VerticalContentAlignment" Value="Center" />
13+
</Style>
14+
15+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}" />
16+
417
<!--
518
Set x:Shared="False" to avoid that only the MenuItem has an icon.
619
See: https://stackoverflow.com/questions/6177550/menuitem-style-with-icon-creates-only-one-icon
720
-->
8-
<Style x:Key="CopyMenuItem" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}"
21+
<Style x:Key="CopyMenuItem" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}"
922
x:Shared="False">
1023
<Setter Property="Icon">
1124
<Setter.Value>

‎Source/NETworkManager/Views/FirewallView.xaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
</controls:MultiSelectDataGrid.InputBindings>
105105
<controls:MultiSelectDataGrid.Resources>
106106
<!-- Enable, disable and delete are blocked if a selected rule is managed by a policy (see FirewallViewModel.IsAnySelectedRuleManagedByPolicy) -->
107-
<Style x:Key="ManagedByPolicyMenuItem" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
107+
<Style x:Key="ManagedByPolicyMenuItem" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
108108
<Style.Resources>
109109
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource WrapToolTip}" />
110110
</Style.Resources>
@@ -152,7 +152,7 @@
152152
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource WrapToolTip}" />
153153
</MenuItem.Resources>
154154
<MenuItem.Style>
155-
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
155+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
156156
<Style.Triggers>
157157
<!-- Rules with unsupported values or not managed by NETworkManager can't be edited (see FirewallViewModel.EditRuleCommand) -->
158158
<DataTrigger Binding="{Binding Path=SelectedResult.HasUnsupportedValues}" Value="True">
@@ -186,7 +186,7 @@
186186
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource WrapToolTip}" />
187187
</MenuItem.Resources>
188188
<MenuItem.Style>
189-
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
189+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
190190
<Style.Triggers>
191191
<!-- Rules with unsupported values can't be copied (see FirewallViewModel.CopyAsRuleCommand) -->
192192
<DataTrigger Binding="{Binding Path=SelectedResult.HasUnsupportedValues}" Value="True">

‎Source/NETworkManager/Views/SettingsGeneralView.xaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<MenuItem Header="{x:Static localization:Strings.SetDefault}"
3434
Command="{Binding Path=SetDefaultApplicationCommand}">
3535
<MenuItem.Style>
36-
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
36+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
3737
<Setter Property="Visibility" Value="Collapsed" />
3838
<Style.Triggers>
3939
<MultiDataTrigger>
@@ -63,7 +63,7 @@
6363
<MenuItem Header="{x:Static localization:Strings.Show}"
6464
Command="{Binding Path=ShowApplicationCommand}">
6565
<MenuItem.Style>
66-
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
66+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
6767
<Setter Property="Visibility" Value="Collapsed" />
6868
<Style.Triggers>
6969
<MultiDataTrigger>
@@ -93,7 +93,7 @@
9393
<MenuItem Header="{x:Static localization:Strings.Hide}"
9494
Command="{Binding Path=HideApplicationCommand}">
9595
<MenuItem.Style>
96-
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
96+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
9797
<Setter Property="Visibility" Value="Collapsed" />
9898
<Style.Triggers>
9999
<MultiDataTrigger>

‎Source/NETworkManager/Views/TracerouteView.xaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@
256256
Command="{Binding Path=PerformRDAPQueryCommand}"
257257
CommandParameter="{Binding SelectedResult.IPAddress}">
258258
<MenuItem.Style>
259-
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MahApps.Styles.MenuItem}">
259+
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource DefaultMenuItem}">
260260
<Style.Triggers>
261261
<!-- Timed out hops have no IP address -->
262262
<DataTrigger Binding="{Binding SelectedResult.IPAddress}" Value="{x:Null}">

‎Website/docs/changelog/next-release.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ Release date: **xx.xx.2026**
365365
## Dependencies, Refactoring & Documentation
366366

367367
- Code cleanup & refactoring
368-
- Context menus of lists (results, profiles, settings) get the view model in XAML instead of when they are opened. Their items no longer cause XAML binding errors every time a list is filled, which hid real binding errors.
368+
- Context menus of lists (results, profiles, settings) get the view model in XAML instead of when they are opened. Their items, and menu items that are created outside of a menu (e.g. the extra items of the profile context menu), no longer cause XAML binding errors every time a list is filled, which hid real binding errors.
369369
- Converted the **IP Scanner** and **Port Scanner** engines from `Parallel.ForEach` with blocking calls to genuinely asynchronous `Parallel.ForEachAsync`, removing the risk of exhausting the application's ThreadPool at high concurrency settings. [#3564](https://github.com/BornToBeRoot/NETworkManager/pull/3564)
370370
- The TCP port probe (**IP Scanner**, **Port Scanner**) and the NetBIOS request (**IP Scanner**) report a refused connection, a timeout or no answer as result instead of throwing an exception for each of them (thousands per scan). Faster, and the application no longer lags while scanning with a debugger attached. [#3653](https://github.com/BornToBeRoot/NETworkManager/pull/3653)
371371
- Refactored the **Network Connection** widget's check logic to reduce duplicated code and share the local IP address detection between the Computer and Router checks instead of detecting it twice. [#3553](https://github.com/BornToBeRoot/NETworkManager/pull/3553)

0 commit comments

Comments
 (0)