完成修改选定变量的启用状态更改

This commit is contained in:
2025-09-02 18:29:58 +08:00
parent 94aa9b0485
commit 5ce3825fa9
6 changed files with 96 additions and 89 deletions

View File

@@ -21,6 +21,7 @@ namespace DMS.WPF.Services
{ typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) }, { typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) },
{ typeof(VariableDialogViewModel), typeof(VariableDialog) }, { typeof(VariableDialogViewModel), typeof(VariableDialog) },
{ typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) }, { typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) },
{ typeof(IsActiveDialogViewModel), typeof(IsActiveDialog) },
// { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here
// ... other dialogs // ... other dialogs
}; };

View File

@@ -1,31 +1,31 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using DMS.Core.Enums;
namespace DMS.WPF.ViewModels.Dialogs; namespace DMS.WPF.ViewModels.Dialogs
public partial class IsActiveDialogViewModel : ObservableObject
{ {
public partial class IsActiveDialogViewModel : DialogViewModelBase<bool?>
{
[ObservableProperty] [ObservableProperty]
private bool? _selectedIsActive; private bool? _selectedIsActive;
public IsActiveDialogViewModel(bool? currentIsActive) public IsActiveDialogViewModel(bool currentIsActive)
{ {
_selectedIsActive = currentIsActive; SelectedIsActive = currentIsActive;
Title = "修改启用状态";
PrimaryButText = "确定";
} }
[RelayCommand] [RelayCommand]
private void SelectIsActive(string isActiveString) private void PrimaryButton()
{ {
if (bool.TryParse(isActiveString, out bool isActive)) Close(SelectedIsActive);
{
SelectedIsActive = isActive;
}
} }
[RelayCommand] [RelayCommand]
private void Cancel() private void CancleButton()
{ {
SelectedIsActive = null; Close(null);
}
} }
} }

View File

@@ -12,6 +12,8 @@ using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ObservableCollections; using ObservableCollections;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DMS.WPF.ViewModels; namespace DMS.WPF.ViewModels;
@@ -638,43 +640,44 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
/// </summary> /// </summary>
/// <param name="variablesToChange">要修改启用状态的变量数据列表。</param> /// <param name="variablesToChange">要修改启用状态的变量数据列表。</param>
[RelayCommand] [RelayCommand]
public async Task ModifyIsActive(IList<object> variablesToChange) public async Task ChangeIsActive()
{ {
// var validVariables = variablesToChange?.OfType<Variable>() // 检查是否有变量被选中
// .ToList(); if (SelectedVariables.Count == 0)
// {
// if (validVariables == null || !validVariables.Any()) NotificationHelper.ShowInfo("请选择要修改启用状态的变量");
// { return;
// NotificationHelper.ShowInfo("请选择要修改启用状态的变量"); }
// return;
// } // 获取选中的变量列表
// var validVariables = SelectedVariables.Cast<VariableItemViewModel>().ToList();
// // 假设所有选中的变量都应该被设置为相同的状态,取第一个变量的当前状态的反值
// var currentIsActive = validVariables.First() // 显示启用状态选择对话框,并传入第一个变量的当前启用状态作为默认值
// .IsActive; IsActiveDialogViewModel viewModel = new IsActiveDialogViewModel(validVariables.First().IsActive);
// var newIsActive = !currentIsActive; var newIsActive = await _dialogService.ShowDialogAsync(viewModel);
// if (newIsActive.HasValue)
// var confirm = await _dialogService.ShowIsActiveDialog(newIsActive); {
// // 更新所有选定变量的启用状态和修改状态
// if (confirm.HasValue && confirm.Value == newIsActive) foreach (var variable in validVariables)
// { {
// foreach (var variable in validVariables) variable.IsActive = newIsActive.Value;
// { variable.UpdatedAt = DateTime.Now;
// variable.IsActive = newIsActive; }
// }
// // 批量更新数据库中的变量数据
// await _varDataRepository.UpdateAsync(validVariables); var variableDtos = _mapper.Map<List<VariableDto>>(validVariables);
// var result = await _variableAppService.UpdateVariablesAsync(variableDtos);
// // 更新界面
// await RefreshDataView(); if (result > 0)
// {
// // 显示成功通知
// NotificationHelper.ShowSuccess($"已成功 {validVariables.Count} 个变量的启用状态修改为 {newIsActive}"); NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的启用状态");
// } }
// else else
// { {
// NotificationHelper.ShowInfo("操作已取消或状态未改变。"); NotificationHelper.ShowError("更新启用状态失败");
// } }
}
} }
/// <summary> /// <summary>

View File

@@ -1,26 +1,35 @@
<controls:ContentDialog x:Class="DMS.WPF.Views.Dialogs.IsActiveDialog" <controls:ContentDialog
x:Class="DMS.WPF.Views.Dialogs.IsActiveDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:vm="clr-namespace:DMS.WPF.ViewModels.Dialogs"
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts" xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
mc:Ignorable="d" xmlns:vm="clr-namespace:DMS.WPF.ViewModels.Dialogs"
Title="{Binding Title}"
d:DataContext="{d:DesignInstance vm:IsActiveDialogViewModel}" d:DataContext="{d:DesignInstance vm:IsActiveDialogViewModel}"
Title="修改激活状态" CloseButtonCommand="{Binding CancleButtonCommand}"
PrimaryButtonText="确定" CloseButtonText="取消"
SecondaryButtonText="取消" PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick" PrimaryButtonText="{Binding PrimaryButText}"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick"> mc:Ignorable="d">
<controls:ContentDialog.Resources> <controls:ContentDialog.Resources>
<valueConverts:NullableBooleanConverter x:Key="NullableBooleanConverter" /> <valueConverts:NullableBooleanConverter x:Key="NullableBooleanConverter" />
</controls:ContentDialog.Resources> </controls:ContentDialog.Resources>
<Grid> <Grid>
<StackPanel Orientation="Vertical" Margin="10"> <StackPanel Margin="10" Orientation="Vertical">
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<RadioButton FontSize="16" Content="启用" IsChecked="{Binding SelectedIsActive, Mode=TwoWay, Converter={StaticResource NullableBooleanConverter}, ConverterParameter=True}" Command="{Binding SelectIsActiveCommand}" CommandParameter="True" Margin="10 5"/> <RadioButton
<RadioButton FontSize="16" Content="停用" IsChecked="{Binding SelectedIsActive, Mode=TwoWay, Converter={StaticResource NullableBooleanConverter}, ConverterParameter=False}" Command="{Binding SelectIsActiveCommand}" CommandParameter="False" Margin="10 5"/> Margin="10,5"
Content="启用"
FontSize="16"
IsChecked="{Binding SelectedIsActive, Mode=TwoWay, Converter={StaticResource NullableBooleanConverter}, ConverterParameter=True}" />
<RadioButton
Margin="10,5"
Content="停用"
FontSize="16"
IsChecked="{Binding SelectedIsActive, Mode=TwoWay, Converter={StaticResource NullableBooleanConverter}, ConverterParameter=False}" />
</StackPanel> </StackPanel>
</StackPanel> </StackPanel>

View File

@@ -5,28 +5,25 @@ namespace DMS.WPF.Views.Dialogs;
public partial class IsActiveDialog : ContentDialog public partial class IsActiveDialog : ContentDialog
{ {
public IsActiveDialogViewModel ViewModel { get; }
public IsActiveDialog(IsActiveDialogViewModel viewModel) public IsActiveDialog()
{ {
InitializeComponent(); InitializeComponent();
ViewModel = viewModel;
DataContext = ViewModel;
} }
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{ {
// 确认按钮点击时ViewModel.SelectedIsActive 已经通过绑定更新 // 确认按钮点击时ViewModel.SelectedIsActive 已经通过绑定更新
// 如果用户没有选择任何选项,可以阻止关闭对话框 // 如果用户没有选择任何选项,可以阻止关闭对话框
if (!ViewModel.SelectedIsActive.HasValue) //if (!ViewModel.SelectedIsActive.HasValue)
{ //{
args.Cancel = true; // args.Cancel = true;
} //}
} }
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{ {
// 取消按钮点击时,将 SelectedIsActive 设置为 null // 取消按钮点击时,将 SelectedIsActive 设置为 null
ViewModel.SelectedIsActive = null; //ViewModel.SelectedIsActive = null;
} }
} }

View File

@@ -196,10 +196,7 @@
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" /> <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" />
</MenuItem.Icon> </MenuItem.Icon>
</MenuItem> </MenuItem>
<MenuItem <MenuItem Command="{Binding ChangeIsActiveCommand}" Header="修改启用状态">
Command="{Binding ModifyIsActiveCommand}"
CommandParameter="{Binding PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
Header="修改启用状态">
<MenuItem.Icon> <MenuItem.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" /> <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" />
</MenuItem.Icon> </MenuItem.Icon>