完成对话框导入变量

This commit is contained in:
2025-09-02 15:02:39 +08:00
parent e0e48d3b71
commit 14144e865c
2 changed files with 25 additions and 22 deletions

View File

@@ -10,6 +10,7 @@ using DMS.Infrastructure.Models;
using DMS.WPF.ViewModels.Items; using DMS.WPF.ViewModels.Items;
using Opc.Ua; using Opc.Ua;
using Opc.Ua.Client; using Opc.Ua.Client;
using System.Collections;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
namespace DMS.WPF.ViewModels.Dialogs; namespace DMS.WPF.ViewModels.Dialogs;
@@ -23,10 +24,10 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
private OpcUaNodeItemViewModel _rootOpcUaNode; private OpcUaNodeItemViewModel _rootOpcUaNode;
[ObservableProperty] [ObservableProperty]
private ObservableCollection<VariableItemViewModel> _selectedNodeVariables; private ObservableCollection<VariableItemViewModel> _opcUaNodeVariables=new ObservableCollection<VariableItemViewModel>();
public List<Variable> SelectedVariables { get; set; } = new List<Variable>();
[ObservableProperty]
private IList _selectedVariables = new ArrayList();
[ObservableProperty] [ObservableProperty]
private bool _selectAllVariables; private bool _selectAllVariables;
@@ -47,7 +48,6 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
public ImportOpcUaDialogViewModel(IOpcUaService opcUaService,IMapper mapper) public ImportOpcUaDialogViewModel(IOpcUaService opcUaService,IMapper mapper)
{ {
SelectedNodeVariables = new ObservableCollection<VariableItemViewModel>();
this._opcUaService = opcUaService; this._opcUaService = opcUaService;
this._mapper = mapper; this._mapper = mapper;
RootOpcUaNode = new OpcUaNodeItemViewModel() { DisplayName = "根节点", NodeId = Objects.ObjectsFolder, IsExpanded = true }; RootOpcUaNode = new OpcUaNodeItemViewModel() { DisplayName = "根节点", NodeId = Objects.ObjectsFolder, IsExpanded = true };
@@ -90,10 +90,20 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
} }
} }
[RelayCommand]
private async void SecondaryButton()
{
await _opcUaService.DisconnectAsync();
Close(SelectedVariables.Cast<VariableItemViewModel>().ToList());
}
[RelayCommand] [RelayCommand]
private async void PrimaryButton() private async void PrimaryButton()
{ {
await _opcUaService.DisconnectAsync(); await _opcUaService.DisconnectAsync();
Close(OpcUaNodeVariables.ToList());
} }
[RelayCommand] [RelayCommand]
@@ -122,20 +132,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
try try
{ {
SelectedNodeVariables.Clear(); OpcUaNodeVariables.Clear();
if (node.NodeClass == NodeClass.Variable)
{
// 如果是变量节点,直接显示它
SelectedNodeVariables.Add(new VariableItemViewModel
{
Name = node.DisplayName,
OpcUaNodeId = node.NodeId.ToString(),
Protocol = ProtocolType.OpcUa,
IsActive = true // 默认选中
});
return;
}
// 加载节点的子项 // 加载节点的子项
node.IsExpanded = true; node.IsExpanded = true;
@@ -147,7 +144,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
var opcNodeItem = _mapper.Map<OpcUaNodeItemViewModel>(children); var opcNodeItem = _mapper.Map<OpcUaNodeItemViewModel>(children);
if (children.NodeClass == NodeClass.Variable) if (children.NodeClass == NodeClass.Variable)
{ {
SelectedNodeVariables.Add(new VariableItemViewModel OpcUaNodeVariables.Add(new VariableItemViewModel
{ {
Name = children.DisplayName, // 修正:使用子节点的显示名称 Name = children.DisplayName, // 修正:使用子节点的显示名称
OpcUaNodeId = children.NodeId.ToString(), OpcUaNodeId = children.NodeId.ToString(),

View File

@@ -4,6 +4,8 @@
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: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:helper="clr-namespace:DMS.WPF.Helper"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:DMS.WPF.ViewModels.Dialogs" xmlns:vm="clr-namespace:DMS.WPF.ViewModels.Dialogs"
Title="从OPC UA服务器导入变量" Title="从OPC UA服务器导入变量"
@@ -11,7 +13,9 @@
CloseButtonCommand="{Binding CloseButtonCommand}" CloseButtonCommand="{Binding CloseButtonCommand}"
CloseButtonText="取消" CloseButtonText="取消"
PrimaryButtonCommand="{Binding PrimaryButtonCommand}" PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
PrimaryButtonText="导入" PrimaryButtonText="导入全部"
SecondaryButtonCommand="{Binding SecondaryButtonCommand}"
SecondaryButtonText="导入选择"
mc:Ignorable="d"> mc:Ignorable="d">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
@@ -88,8 +92,10 @@
Grid.Column="1" Grid.Column="1"
AutoGenerateColumns="False" AutoGenerateColumns="False"
IsReadOnly="True" IsReadOnly="True"
ItemsSource="{Binding SelectedNodeVariables}" ItemsSource="{Binding OpcUaNodeVariables}">
SelectionChanged="Selector_OnSelectionChanged"> <i:Interaction.Behaviors>
<helper:SelectedItemsBehavior SelectedItems="{Binding SelectedVariables}" />
</i:Interaction.Behaviors>
<DataGrid.Style> <DataGrid.Style>
<Style BasedOn="{StaticResource {x:Type DataGrid}}" TargetType="DataGrid"> <Style BasedOn="{StaticResource {x:Type DataGrid}}" TargetType="DataGrid">
<Setter Property="Visibility" Value="Collapsed" /> <Setter Property="Visibility" Value="Collapsed" />