Skip to content

Commit 77b03cd

Browse files
committed
fix navigation issues
1 parent 95fa75d commit 77b03cd

8 files changed

Lines changed: 163 additions & 191 deletions

File tree

src/Modulus.App/App.axaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ private void ConfigureServices()
120120
services.AddTransient<DashboardViewModel>();
121121
services.AddTransient<PluginManagerViewModel>();
122122
services.AddTransient<SettingsViewModel>();
123-
services.AddTransient<MainWindowViewModel>();
124-
services.AddTransient<MainWindowViewModel>();
125123

126124
// 创建服务提供器
127125
_serviceProvider = services.BuildServiceProvider();

src/Modulus.App/Controls/NavigationBody.axaml

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<UserControl xmlns="https://github.com/avaloniaui"
32
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
43
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
54
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
65
xmlns:vm="clr-namespace:Modulus.App.Controls.ViewModels"
76
xmlns:conv="clr-namespace:Modulus.App.Converters"
7+
xmlns:controls="clr-namespace:Modulus.App.Controls"
88
mc:Ignorable="d" d:DesignWidth="220" d:DesignHeight="400"
99
x:Class="Modulus.App.Controls.NavigationBody"
1010
x:Name="control"
11-
x:DataType="vm:NavigationViewModel">
11+
x:DataType="vm:NavigationViewModel"
12+
DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:NavigationView}, Path=DataContext}">
1213
<Design.DataContext>
1314
<vm:NavigationViewModel />
1415
</Design.DataContext>
@@ -18,28 +19,41 @@
1819
</UserControl.Resources>
1920

2021
<UserControl.Styles>
21-
<!-- 当控件未展开时,隐藏所有标记为label的TextBlock -->
22-
<Style Selector="UserControl:not(.expanded) TextBlock.label">
23-
<Setter Property="IsVisible" Value="False" />
24-
</Style>
25-
<!-- 确保展开时的样式 -->
26-
<Style Selector="UserControl.expanded TextBlock.label">
27-
<Setter Property="IsVisible" Value="True" />
28-
</Style>
29-
30-
<!-- 折叠状态下的按钮样式 -->
31-
<Style Selector="UserControl:not(.expanded) Button">
22+
<!-- 默认按钮样式 -->
23+
<Style Selector="Button.nav-button">
24+
<Setter Property="Height" Value="36" />
25+
<Setter Property="Margin" Value="0,2" />
3226
<Setter Property="CornerRadius" Value="8" />
27+
<Setter Property="HorizontalAlignment" Value="Stretch" />
28+
</Style>
29+
30+
<!-- 折叠状态按钮样式 -->
31+
<Style Selector="Button.collapsed">
3332
<Setter Property="Width" Value="36" />
34-
<Setter Property="Height" Value="36" />
35-
<Setter Property="HorizontalContentAlignment" Value="Center" />
3633
<Setter Property="Padding" Value="8" />
34+
<Setter Property="HorizontalAlignment" Value="Center" />
35+
<Setter Property="HorizontalContentAlignment" Value="Center" />
36+
</Style>
37+
38+
<!-- 展开状态按钮样式 -->
39+
<Style Selector="Button.expanded">
40+
<Setter Property="Padding" Value="12,8" />
41+
<Setter Property="HorizontalContentAlignment" Value="Left" />
42+
<Setter Property="Width" Value="196" />
43+
</Style>
44+
45+
<!-- 标签文本样式 -->
46+
<Style Selector="TextBlock.nav-label">
47+
<Setter Property="IsVisible" Value="False" />
48+
</Style>
49+
50+
<Style Selector="TextBlock.nav-label.expanded">
51+
<Setter Property="IsVisible" Value="True" />
3752
</Style>
3853
</UserControl.Styles>
3954

4055
<Grid>
4156
<Grid.RowDefinitions>
42-
<RowDefinition Height="Auto"/>
4357
<RowDefinition Height="Auto"/>
4458
<RowDefinition Height="*"/>
4559
</Grid.RowDefinitions>
@@ -50,77 +64,70 @@
5064
Background="#333333"
5165
Margin="16,0,16,12"/>
5266

53-
<!-- DEBUG: 显示NavigationItems数量 -->
54-
<TextBlock Grid.Row="1"
55-
Text="{Binding NavigationItems.Count, StringFormat='菜单项: {0}个'}"
56-
Foreground="White"
57-
HorizontalAlignment="Center"
58-
Margin="0,0,0,8"/>
59-
6067
<!-- 导航菜单项列表 -->
61-
<ScrollViewer Grid.Row="2"
68+
<ScrollViewer Grid.Row="1"
6269
HorizontalScrollBarVisibility="Hidden"
6370
VerticalScrollBarVisibility="Auto">
6471
<ItemsControl x:Name="NavigationItemsControl"
6572
ItemsSource="{Binding NavigationItems}"
6673
Margin="12,0"
67-
HorizontalAlignment="Center">
74+
HorizontalAlignment="Stretch">
6875
<ItemsControl.ItemsPanel>
6976
<ItemsPanelTemplate>
70-
<StackPanel Spacing="8"/>
77+
<StackPanel Spacing="4"/>
7178
</ItemsPanelTemplate>
7279
</ItemsControl.ItemsPanel>
7380
<ItemsControl.ItemTemplate>
7481
<DataTemplate DataType="{x:Type vm:NavigationItemModel}">
75-
<Button Background="{Binding IsActive, Converter={StaticResource NavItemBackgroundConverter}}"
76-
Foreground="White"
77-
ToolTip.Tip="{Binding Label}"
78-
Command="{Binding $parent[UserControl].((vm:NavigationViewModel)DataContext).NavigateToViewCommand}"
79-
CommandParameter="{Binding}"
80-
HorizontalAlignment="Stretch"
81-
HorizontalContentAlignment="Left"
82-
CornerRadius="8"
83-
Margin="0,0,0,4"
84-
Padding="12,8">
85-
<Grid>
86-
<Grid.ColumnDefinitions>
87-
<ColumnDefinition Width="Auto"/>
88-
<ColumnDefinition Width="*"/>
89-
</Grid.ColumnDefinitions>
90-
91-
<!-- 图标 -->
92-
<TextBlock Text="{Binding Icon}"
93-
Grid.Column="0"
94-
FontFamily="Segoe MDL2 Assets"
95-
FontSize="18"
96-
VerticalAlignment="Center"
97-
HorizontalAlignment="Center"/>
82+
<Grid>
83+
<Button Classes="nav-button"
84+
Classes.collapsed="{Binding !$parent[UserControl].((vm:NavigationViewModel)DataContext).IsNavigationExpanded}"
85+
Classes.expanded="{Binding $parent[UserControl].((vm:NavigationViewModel)DataContext).IsNavigationExpanded}"
86+
Background="{Binding IsActive, Converter={StaticResource NavItemBackgroundConverter}}"
87+
Foreground="White"
88+
ToolTip.Tip="{Binding Label}"
89+
Command="{Binding $parent[UserControl].((vm:NavigationViewModel)DataContext).NavigateToViewCommand}"
90+
CommandParameter="{Binding}">
91+
<Grid>
92+
<Grid.ColumnDefinitions>
93+
<ColumnDefinition Width="Auto"/>
94+
<ColumnDefinition Width="*"/>
95+
</Grid.ColumnDefinitions>
9896

99-
<!-- 标签文本 - 使用样式类控制可见性 -->
100-
<TextBlock Text="{Binding Label}"
101-
Classes="label"
102-
Grid.Column="1"
103-
Margin="10,0,0,0"
104-
VerticalAlignment="Center"/>
105-
106-
<!-- 徽章 - 调整徽章大小 -->
107-
<Border IsVisible="{Binding HasBadge}"
108-
Grid.Column="1"
109-
Background="#FF3B30"
110-
CornerRadius="6"
111-
Width="12" Height="12"
112-
HorizontalAlignment="Right"
113-
VerticalAlignment="Top"
114-
Margin="0,0,4,0">
115-
<TextBlock Text="{Binding BadgeText}"
116-
FontSize="8"
117-
FontWeight="Bold"
118-
HorizontalAlignment="Center"
119-
VerticalAlignment="Center"
120-
Foreground="White"/>
121-
</Border>
122-
</Grid>
123-
</Button>
97+
<!-- 图标 -->
98+
<TextBlock Text="{Binding Icon}"
99+
Grid.Column="0"
100+
FontFamily="Segoe MDL2 Assets"
101+
FontSize="18"
102+
VerticalAlignment="Center"
103+
HorizontalAlignment="Center"/>
104+
105+
<!-- 标签文本 -->
106+
<TextBlock Text="{Binding Label}"
107+
Classes="nav-label"
108+
Classes.expanded="{Binding $parent[UserControl].((vm:NavigationViewModel)DataContext).IsNavigationExpanded}"
109+
Grid.Column="1"
110+
Margin="10,0,0,0"
111+
VerticalAlignment="Center"/>
112+
</Grid>
113+
</Button>
114+
115+
<!-- 徽章 -->
116+
<Border IsVisible="{Binding HasBadge}"
117+
Background="#FF3B30"
118+
CornerRadius="6"
119+
Width="12" Height="12"
120+
HorizontalAlignment="Right"
121+
VerticalAlignment="Top"
122+
Margin="0,0,0,0">
123+
<TextBlock Text="{Binding BadgeText}"
124+
FontSize="8"
125+
FontWeight="Bold"
126+
HorizontalAlignment="Center"
127+
VerticalAlignment="Center"
128+
Foreground="White"/>
129+
</Border>
130+
</Grid>
124131
</DataTemplate>
125132
</ItemsControl.ItemTemplate>
126133
</ItemsControl>

src/Modulus.App/Controls/NavigationBody.axaml.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,7 @@ public bool IsExpanded
3030
set => SetValue(IsExpandedProperty, value);
3131
}
3232

33-
/// <summary>
34-
/// 定义 NavigationItems 依赖属性
35-
/// </summary>
36-
public static readonly StyledProperty<ObservableCollection<NavigationItemModel>> NavigationItemsProperty =
37-
AvaloniaProperty.Register<NavigationBody, ObservableCollection<NavigationItemModel>>(
38-
nameof(NavigationItems), new ObservableCollection<NavigationItemModel>());
39-
40-
/// <summary>
41-
/// 获取或设置导航项列表
42-
/// </summary>
43-
public ObservableCollection<NavigationItemModel> NavigationItems
44-
{
45-
get => GetValue(NavigationItemsProperty);
46-
set => SetValue(NavigationItemsProperty, value);
47-
}
33+
// Removed NavigationItemsProperty and NavigationItems property
4834

4935
#endregion
5036

@@ -79,12 +65,6 @@ private void OnDataContextChanged(object? sender, EventArgs e)
7965
{
8066
// 更新导航命令
8167
this.SetValue(NavigateToViewCommandProperty, viewModel.NavigateToViewCommand);
82-
83-
// 更新导航项列表 - 重要:确保菜单项数据正确传递
84-
if (viewModel.NavigationItems != null)
85-
{
86-
this.NavigationItems = viewModel.NavigationItems;
87-
}
8868
}
8969
}
9070

0 commit comments

Comments
 (0)