From f61c50577436a94511620a9a0cceb000a3c6746a Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Mon, 22 Sep 2025 23:33:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=99=A8=E7=9A=84=E5=AF=B9=E8=AF=9D=E6=A1=86?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/InitializeRepository.cs | 1 + .../Repositories/TriggerRepository.cs | 2 +- .../Converters/IntToVisibilityConverter.cs | 35 ++++++ .../Dialogs/TriggerDialogViewModel.cs | 74 +++++++++++-- DMS.WPF/Views/Dialogs/TriggerDialog.xaml | 100 ++++++++++++++---- DMS.WPF/Views/Dialogs/TriggerDialog.xaml.cs | 26 +++++ 6 files changed, 207 insertions(+), 31 deletions(-) create mode 100644 DMS.WPF/Converters/IntToVisibilityConverter.cs diff --git a/DMS.Infrastructure/Repositories/InitializeRepository.cs b/DMS.Infrastructure/Repositories/InitializeRepository.cs index 89f08aa..11297b3 100644 --- a/DMS.Infrastructure/Repositories/InitializeRepository.cs +++ b/DMS.Infrastructure/Repositories/InitializeRepository.cs @@ -52,6 +52,7 @@ public class InitializeRepository : IInitializeRepository _db.CodeFirst.InitTables(); _db.CodeFirst.InitTables(); _db.CodeFirst.InitTables(); + _db.CodeFirst.InitTables(); } /// diff --git a/DMS.Infrastructure/Repositories/TriggerRepository.cs b/DMS.Infrastructure/Repositories/TriggerRepository.cs index ec45702..cd1c199 100644 --- a/DMS.Infrastructure/Repositories/TriggerRepository.cs +++ b/DMS.Infrastructure/Repositories/TriggerRepository.cs @@ -83,7 +83,7 @@ namespace DMS.Infrastructure.Repositories VariableId = variableId }).ToList(); - await _dbContext.GetInstance().Insertable(triggerVariables).ExecuteCommandAsync(); + await _dbContext.GetInstance().Insertable(triggerVariables).ExecuteCommandAsync(); } return _mapper.Map(dbTrigger, trigger); diff --git a/DMS.WPF/Converters/IntToVisibilityConverter.cs b/DMS.WPF/Converters/IntToVisibilityConverter.cs new file mode 100644 index 0000000..90b4d00 --- /dev/null +++ b/DMS.WPF/Converters/IntToVisibilityConverter.cs @@ -0,0 +1,35 @@ +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace DMS.WPF.Converters +{ + /// + /// 将整数值转换为Visibility的转换器。 + /// 当值等于参数时,返回Visibility.Collapsed;否则返回Visibility.Visible。 + /// + public class IntToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is int intValue && parameter is string paramString && int.TryParse(paramString, out int paramValue)) + { + return intValue == paramValue ? Visibility.Collapsed : Visibility.Visible; + } + + // 默认情况下,如果值为0则隐藏,否则显示 + if (value is int intValueDefault) + { + return intValueDefault == 0 ? Visibility.Collapsed : Visibility.Visible; + } + + return Visibility.Visible; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs index 74c6f93..29cbef3 100644 --- a/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/TriggerDialogViewModel.cs @@ -1,4 +1,5 @@ using System.Collections.ObjectModel; +using System.ComponentModel; using System.Text.Json; using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; @@ -30,10 +31,13 @@ namespace DMS.WPF.ViewModels.Dialogs private TriggerDefinitionDto _trigger = new(); [ObservableProperty] - private List _availableVariables = new(); + private List _availableVariables = new(); [ObservableProperty] private ObservableCollection _selectedVariables = new(); + + [ObservableProperty] + private ObservableCollection _filteredVariables = new(); // Properties for easier binding in XAML for SendEmail action config [ObservableProperty] @@ -59,17 +63,65 @@ namespace DMS.WPF.ViewModels.Dialogs partial void OnSearchTextChanged(string searchText) { - SelectedVariables.Clear(); - foreach (var variableKv in _dataStorageService.Variables) + UpdateFilteredVariables(); + } + + private void UpdateFilteredVariables() + { + FilteredVariables.Clear(); + + // 如果没有搜索文本,显示所有可用变量 + if (string.IsNullOrEmpty(SearchText)) { - if (variableKv.Value.Name.Contains(SearchText)) + foreach (var variable in AvailableVariables) { - SelectedVariables.Add(variableKv.Value); + // 只显示未被选中的变量 + if (!SelectedVariables.Contains(variable)) + { + FilteredVariables.Add(variable); + } + } + } + else + { + // 根据搜索文本过滤变量 + foreach (var variable in AvailableVariables) + { + // 只显示未被选中的变量且名称包含搜索文本的变量 + if (!SelectedVariables.Contains(variable) && + variable.Name.Contains(SearchText, StringComparison.OrdinalIgnoreCase)) + { + FilteredVariables.Add(variable); + } } } } + /// + /// 将变量添加到选中列表 + /// + /// 要添加的变量 + public void AddVariable(VariableItemViewModel variable) + { + if (!SelectedVariables.Contains(variable)) + { + SelectedVariables.Add(variable); + UpdateFilteredVariables(); + } + } + /// + /// 从选中列表中移除变量 + /// + /// 要移除的变量 + public void RemoveVariable(VariableItemViewModel variable) + { + if (SelectedVariables.Contains(variable)) + { + SelectedVariables.Remove(variable); + UpdateFilteredVariables(); + } + } /// /// 初始化视图模型(传入待编辑的触发器) @@ -94,10 +146,13 @@ namespace DMS.WPF.ViewModels.Dialogs var variable = AvailableVariables.FirstOrDefault(v => v.Id == variableId); if (variable != null) { - // SelectedVariables.Add(variable); + SelectedVariables.Add(variable); } } } + + // 初始化过滤后的变量列表 + UpdateFilteredVariables(); // Parse action configuration if it's SendEmail if (Trigger.Action == ActionType.SendEmail && !string.IsNullOrEmpty(Trigger.ActionConfigurationJson)) @@ -131,13 +186,14 @@ namespace DMS.WPF.ViewModels.Dialogs { try { - var variables = await _variableAppService.GetAllVariablesAsync(); - AvailableVariables = variables ?? new List(); + // 使用数据存储服务中的变量列表 + AvailableVariables = new List(_dataStorageService.Variables.Select(kvp => kvp.Value)); + UpdateFilteredVariables(); } catch (Exception ex) { _notificationService.ShowError($"加载变量列表失败: {ex.Message}"); - AvailableVariables = new List(); + AvailableVariables = new List(); } } diff --git a/DMS.WPF/Views/Dialogs/TriggerDialog.xaml b/DMS.WPF/Views/Dialogs/TriggerDialog.xaml index bde075b..5b9c19e 100644 --- a/DMS.WPF/Views/Dialogs/TriggerDialog.xaml +++ b/DMS.WPF/Views/Dialogs/TriggerDialog.xaml @@ -9,13 +9,14 @@ xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:ex="clr-namespace:DMS.Extensions" xmlns:converters="clr-namespace:DMS.WPF.Converters" + xmlns:items="clr-namespace:DMS.WPF.ViewModels.Items" Title="{Binding Title}" mc:Ignorable="d" - d:DesignHeight="600" + d:DesignHeight="700" d:DesignWidth="600" d:DataContext="{d:DesignInstance vmd:TriggerDialogViewModel}" MinWidth="500" - MinHeight="500" + MinHeight="600" PrimaryButtonText="{Binding PrimaryButText}" CloseButtonText="取消" PrimaryButtonCommand="{Binding SaveCommand}" @@ -24,6 +25,7 @@ + - - - - - - - - - - - - - -