diff --git a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs index 9e7859c..1414ac4 100644 --- a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs +++ b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.ObjectModel; using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; @@ -38,7 +39,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable /// 建议的变量列表 /// [ObservableProperty] - private List _suggestedVariables; + private ObservableCollection _suggestedVariables; /// /// 历史记录条数限制 @@ -57,6 +58,12 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable /// [ObservableProperty] private DateTime? _endTime; + + /// + /// 选中的变量历史记录 + /// + [ObservableProperty] + private VariableHistoryDto _selectedVariable; /// /// 变量历史记录列表 @@ -85,12 +92,13 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable _variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v); VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged(); _allVariableHistories = new List(); - _suggestedVariables = new List(); + _suggestedVariables = new ObservableCollection(); // 初始化默认值 _historyLimit = 1000; // 默认限制1000条记录 _startTime = null; _endTime = null; + _selectedVariable = new VariableHistoryDto(); } /// @@ -119,44 +127,80 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable } /// - /// 更新建议的变量列表 - /// - private void UpdateSuggestedVariables() +/// 更新建议的变量列表 +/// +private void UpdateSuggestedVariables() +{ + // 清空现有建议列表 + _suggestedVariables.Clear(); + + if (string.IsNullOrWhiteSpace(SearchText)) { - if (string.IsNullOrWhiteSpace(SearchText)) - { - // 如果搜索文本为空,显示所有唯一的变量名 - _suggestedVariables = _allVariableHistories - .GroupBy(h => h.VariableName) - .Select(g => g.First()) - .Take(10) - .ToList(); - } - else - { - // 根据搜索文本过滤建议列表 - _suggestedVariables = _allVariableHistories - .Where(h => - h.VariableName?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) == - true) - .GroupBy(h => h.VariableName) - .Select(g => g.First()) - .Take(10) - .ToList(); - } + // 如果搜索文本为空,显示所有唯一的变量名 + var uniqueVariables = _allVariableHistories + .GroupBy(h => h.VariableName) + .Select(g => g.First()) + .Take(10) + .ToList(); + foreach (var variable in uniqueVariables) + { + _suggestedVariables.Add(variable); + } } + else + { + // 根据搜索文本过滤建议列表 + var filteredVariables = _allVariableHistories + .Where(h => + h.VariableName?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) == + true) + .GroupBy(h => h.VariableName) + .Select(g => g.First()) + .Take(10) + .ToList(); + foreach (var variable in filteredVariables) + { + _suggestedVariables.Add(variable); + } + } +} + + public async Task OnNavigatedToAsync(MenuItemViewModel menu) + { + // 加载所有变量的历史记录 + LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); + } + /// - /// 搜索变量历史记录 + /// 重新加载历史记录命令 + /// + [RelayCommand] + private void Reload() + { + LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); + } + + /// + /// 更新建议列表命令 + /// + [RelayCommand] + private void UpdateSuggestions() + { + UpdateSuggestedVariables(); + } + + /// + /// 当搜索文本改变时触发 /// /// partial void OnSearchTextChanged(string value) { // 更新建议列表 UpdateSuggestedVariables(); - - if (string.IsNullOrWhiteSpace(SearchText)) + + if (string.IsNullOrWhiteSpace(value)) { // 如果搜索文本为空,显示所有历史记录 _variableHistoryList.Clear(); @@ -168,18 +212,38 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable var filteredHistories = _allVariableHistories .Where(h => h.VariableName?.Contains( - SearchText, StringComparison.OrdinalIgnoreCase) == true) + value, StringComparison.OrdinalIgnoreCase) == true) .ToList(); _variableHistoryList.Clear(); _variableHistoryList.AddRange(filteredHistories); } } - - public async Task OnNavigatedToAsync(MenuItemViewModel menu) + + /// + /// 根据搜索文本过滤历史记录 + /// + /// + private void FilterHistoriesBySearchText(string searchText) { - // 加载所有变量的历史记录 - LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); + if (string.IsNullOrWhiteSpace(searchText)) + { + // 如果搜索文本为空,显示所有历史记录 + _variableHistoryList.Clear(); + _variableHistoryList.AddRange(_allVariableHistories); + } + else + { + // 根据搜索文本过滤历史记录 + var filteredHistories = _allVariableHistories + .Where(h => + h.VariableName?.Contains( + searchText, StringComparison.OrdinalIgnoreCase) == true) + .ToList(); + + _variableHistoryList.Clear(); + _variableHistoryList.AddRange(filteredHistories); + } } /// diff --git a/DMS.WPF/Views/VariableHistoryView.xaml b/DMS.WPF/Views/VariableHistoryView.xaml index 26e91ea..9cccca7 100644 --- a/DMS.WPF/Views/VariableHistoryView.xaml +++ b/DMS.WPF/Views/VariableHistoryView.xaml @@ -6,6 +6,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core" xmlns:ex="clr-namespace:DMS.Extensions" + xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:helper="clr-namespace:DMS.WPF.Helper" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" @@ -48,6 +49,7 @@ Spacing="10"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +