Просмотр исходного кода

修复导航栏选中状态 - 点击后正确高亮当前Tab

磊 曹 3 месяцев назад
Родитель
Сommit
4d0bcc5edc

+ 27 - 0
src/YZWater.Avalonia/Converters/BoolConverters.cs

@@ -0,0 +1,27 @@
+using Avalonia.Data.Converters;
+using Avalonia.Media;
+using System;
+using System.Globalization;
+
+namespace YZWater.Avalonia.Converters;
+
+/// <summary>
+/// 布尔值转换器集合
+/// </summary>
+public static class BoolConverters
+{
+    /// <summary>
+    /// true → #00897B (Success绿), false → #6B7280 (灰色)
+    /// </summary>
+    public static readonly IValueConverter ToBrush =
+        new FuncValueConverter<bool, IBrush>(active =>
+            active ? new SolidColorBrush(Color.Parse("#00897B"))
+                   : new SolidColorBrush(Color.Parse("#6B7280")));
+
+    /// <summary>
+    /// true → Bold, false → Normal
+    /// </summary>
+    public static readonly IValueConverter ToFontWeight =
+        new FuncValueConverter<bool, FontWeight>(active =>
+            active ? FontWeight.Bold : FontWeight.Normal);
+}

+ 94 - 16
src/YZWater.Avalonia/Views/MainWindow.axaml

@@ -2,6 +2,7 @@
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="using:YZWater.Core.ViewModels"
         xmlns:views="using:YZWater.Avalonia.Views"
+        xmlns:conv="using:YZWater.Avalonia.Converters"
         x:Class="YZWater.Avalonia.Views.MainWindow"
         x:DataType="vm:MainViewModel"
         Title="YZ WATER - 污水处理监控系统"
@@ -14,30 +15,107 @@
         <vm:MainViewModel/>
     </Window.DataContext>
 
-    <!-- 直接使用 DockPanel 填满窗口,不使用 AdaptiveContainer -->
     <DockPanel>
         <!-- 底部导航栏 -->
         <Border DockPanel.Dock="Bottom" Background="#111820" BorderBrush="#1C2333" BorderThickness="0,1,0,0" Padding="4,0">
             <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Spacing="0">
-                <Button Content="PROCESS" Command="{Binding ShowViewACommand}"
-                        FontFamily="Consolas, monospace" FontSize="11" Padding="20,8"
-                        Background="Transparent" Foreground="#00897B" BorderThickness="0"/>
+                <!-- PROCESS -->
+                <Button Command="{Binding ShowViewACommand}" Padding="20,8" BorderThickness="0" Cursor="Hand">
+                    <Button.Content>
+                        <StackPanel Orientation="Horizontal" Spacing="6">
+                            <Border Width="3" Height="14" CornerRadius="1"
+                                    Background="{Binding IsTabAActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                            <TextBlock Text="PROCESS" FontFamily="Consolas, monospace" FontSize="11"
+                                       FontWeight="{Binding IsTabAActive, Converter={x:Static conv:BoolConverters.ToFontWeight}}"
+                                       Foreground="{Binding IsTabAActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                        </StackPanel>
+                    </Button.Content>
+                    <Button.Styles>
+                        <Style Selector="Button">
+                            <Setter Property="Background" Value="Transparent"/>
+                        </Style>
+                    </Button.Styles>
+                </Button>
+
                 <Border Background="#1C2333" Width="1" Height="20" VerticalAlignment="Center"/>
-                <Button Content="PARAMS" Command="{Binding ShowViewBCommand}"
-                        FontFamily="Consolas, monospace" FontSize="11" Padding="20,8"
-                        Background="Transparent" Foreground="#9CA3AF" BorderThickness="0"/>
+
+                <!-- PARAMS -->
+                <Button Command="{Binding ShowViewBCommand}" Padding="20,8" BorderThickness="0" Cursor="Hand">
+                    <Button.Content>
+                        <StackPanel Orientation="Horizontal" Spacing="6">
+                            <Border Width="3" Height="14" CornerRadius="1"
+                                    Background="{Binding IsTabBActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                            <TextBlock Text="PARAMS" FontFamily="Consolas, monospace" FontSize="11"
+                                       FontWeight="{Binding IsTabBActive, Converter={x:Static conv:BoolConverters.ToFontWeight}}"
+                                       Foreground="{Binding IsTabBActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                        </StackPanel>
+                    </Button.Content>
+                    <Button.Styles>
+                        <Style Selector="Button">
+                            <Setter Property="Background" Value="Transparent"/>
+                        </Style>
+                    </Button.Styles>
+                </Button>
+
                 <Border Background="#1C2333" Width="1" Height="20" VerticalAlignment="Center"/>
-                <Button Content="FLOW" Command="{Binding ShowViewCCommand}"
-                        FontFamily="Consolas, monospace" FontSize="11" Padding="20,8"
-                        Background="Transparent" Foreground="#9CA3AF" BorderThickness="0"/>
+
+                <!-- FLOW -->
+                <Button Command="{Binding ShowViewCCommand}" Padding="20,8" BorderThickness="0" Cursor="Hand">
+                    <Button.Content>
+                        <StackPanel Orientation="Horizontal" Spacing="6">
+                            <Border Width="3" Height="14" CornerRadius="1"
+                                    Background="{Binding IsTabCActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                            <TextBlock Text="FLOW" FontFamily="Consolas, monospace" FontSize="11"
+                                       FontWeight="{Binding IsTabCActive, Converter={x:Static conv:BoolConverters.ToFontWeight}}"
+                                       Foreground="{Binding IsTabCActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                        </StackPanel>
+                    </Button.Content>
+                    <Button.Styles>
+                        <Style Selector="Button">
+                            <Setter Property="Background" Value="Transparent"/>
+                        </Style>
+                    </Button.Styles>
+                </Button>
+
                 <Border Background="#1C2333" Width="1" Height="20" VerticalAlignment="Center"/>
-                <Button Content="ALARM" Command="{Binding ShowViewDCommand}"
-                        FontFamily="Consolas, monospace" FontSize="11" Padding="20,8"
-                        Background="Transparent" Foreground="#9CA3AF" BorderThickness="0"/>
+
+                <!-- ALARM -->
+                <Button Command="{Binding ShowViewDCommand}" Padding="20,8" BorderThickness="0" Cursor="Hand">
+                    <Button.Content>
+                        <StackPanel Orientation="Horizontal" Spacing="6">
+                            <Border Width="3" Height="14" CornerRadius="1"
+                                    Background="{Binding IsTabDActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                            <TextBlock Text="ALARM" FontFamily="Consolas, monospace" FontSize="11"
+                                       FontWeight="{Binding IsTabDActive, Converter={x:Static conv:BoolConverters.ToFontWeight}}"
+                                       Foreground="{Binding IsTabDActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                        </StackPanel>
+                    </Button.Content>
+                    <Button.Styles>
+                        <Style Selector="Button">
+                            <Setter Property="Background" Value="Transparent"/>
+                        </Style>
+                    </Button.Styles>
+                </Button>
+
                 <Border Background="#1C2333" Width="1" Height="20" VerticalAlignment="Center"/>
-                <Button Content="ABOUT" Command="{Binding ShowViewECommand}"
-                        FontFamily="Consolas, monospace" FontSize="11" Padding="20,8"
-                        Background="Transparent" Foreground="#9CA3AF" BorderThickness="0"/>
+
+                <!-- ABOUT -->
+                <Button Command="{Binding ShowViewECommand}" Padding="20,8" BorderThickness="0" Cursor="Hand">
+                    <Button.Content>
+                        <StackPanel Orientation="Horizontal" Spacing="6">
+                            <Border Width="3" Height="14" CornerRadius="1"
+                                    Background="{Binding IsTabEActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                            <TextBlock Text="ABOUT" FontFamily="Consolas, monospace" FontSize="11"
+                                       FontWeight="{Binding IsTabEActive, Converter={x:Static conv:BoolConverters.ToFontWeight}}"
+                                       Foreground="{Binding IsTabEActive, Converter={x:Static conv:BoolConverters.ToBrush}}"/>
+                        </StackPanel>
+                    </Button.Content>
+                    <Button.Styles>
+                        <Style Selector="Button">
+                            <Setter Property="Background" Value="Transparent"/>
+                        </Style>
+                    </Button.Styles>
+                </Button>
             </StackPanel>
         </Border>
 

+ 22 - 49
src/YZWater.Core/ViewModels/MainViewModel.cs

@@ -31,6 +31,13 @@ public partial class MainViewModel : ObservableObject
     [ObservableProperty]
     private int _selectedTabIndex;
 
+    // 各 Tab 激活状态
+    [ObservableProperty] private bool _isTabAActive = true;
+    [ObservableProperty] private bool _isTabBActive;
+    [ObservableProperty] private bool _isTabCActive;
+    [ObservableProperty] private bool _isTabDActive;
+    [ObservableProperty] private bool _isTabEActive;
+
     public MainViewModel()
     {
         _viewAViewModel = new ViewAViewModel();
@@ -40,88 +47,54 @@ public partial class MainViewModel : ObservableObject
         _viewEViewModel = new ViewEViewModel();
 
         _currentView = _viewAViewModel;
-
-        // 启动定时器更新时间
         StartTimer();
     }
 
-    /// <summary>
-    /// 切换到视图 A (主工艺)
-    /// </summary>
+    private void SetActiveTab(int index)
+    {
+        SelectedTabIndex = index;
+        IsTabAActive = index == 0;
+        IsTabBActive = index == 1;
+        IsTabCActive = index == 2;
+        IsTabDActive = index == 3;
+        IsTabEActive = index == 4;
+    }
+
     [RelayCommand]
     private void ShowViewA()
     {
         CurrentView = _viewAViewModel;
-        SelectedTabIndex = 0;
-        Log.Debug("切换到主工艺视图");
+        SetActiveTab(0);
     }
 
-    /// <summary>
-    /// 切换到视图 B (参数设置)
-    /// </summary>
     [RelayCommand]
     private void ShowViewB()
     {
         CurrentView = _viewBViewModel;
-        SelectedTabIndex = 1;
-        Log.Debug("切换到参数设置视图");
+        SetActiveTab(1);
     }
 
-    /// <summary>
-    /// 切换到视图 C (流量记录)
-    /// </summary>
     [RelayCommand]
     private void ShowViewC()
     {
         CurrentView = _viewCViewModel;
-        SelectedTabIndex = 2;
-        Log.Debug("切换到流量记录视图");
+        SetActiveTab(2);
     }
 
-    /// <summary>
-    /// 切换到视图 D (报警记录)
-    /// </summary>
     [RelayCommand]
     private void ShowViewD()
     {
         CurrentView = _viewDViewModel;
-        SelectedTabIndex = 3;
-        Log.Debug("切换到报警记录视图");
+        SetActiveTab(3);
     }
 
-    /// <summary>
-    /// 切换到视图 E (关于)
-    /// </summary>
     [RelayCommand]
     private void ShowViewE()
     {
         CurrentView = _viewEViewModel;
-        SelectedTabIndex = 4;
-        Log.Debug("切换到关于视图");
-    }
-
-    /// <summary>
-    /// 连接 PLC
-    /// </summary>
-    [RelayCommand]
-    private async Task ConnectPlcAsync()
-    {
-        IsPlcConnected = await PlcService.ConnectAsync();
-    }
-
-    /// <summary>
-    /// 断开 PLC
-    /// </summary>
-    [RelayCommand]
-    private void DisconnectPlc()
-    {
-        PlcService.Disconnect();
-        IsPlcConnected = false;
+        SetActiveTab(4);
     }
 
-    /// <summary>
-    /// 启动定时器
-    /// </summary>
     private void StartTimer()
     {
         var timer = new System.Timers.Timer(1000);