From 6d2bc57923fa1301d33a3687f0750e51774938d0 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 23 Aug 2025 20:52:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ContextDialog=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=A1=86=E5=86=85=E5=AE=B9=E5=8C=BA=E5=9F=9F=E7=9A=84?= =?UTF-8?q?MaxWidth=E5=92=8CMaxHeight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/Helper/VisualTreeFinder.cs | 90 ++++++++++++++++++++ DMS.WPF/Services/DialogService.cs | 2 +- DMS.WPF/ViewModels/VariableTableViewModel.cs | 13 +-- DMS.WPF/Views/Dialogs/DeviceDialog.xaml | 2 +- DMS.WPF/Views/Dialogs/DeviceDialog.xaml.cs | 28 +++--- DMS.WPF/Views/Dialogs/VariableDialog.xaml | 11 +-- DMS.WPF/Views/Dialogs/VariableDialog.xaml.cs | 15 +++- 7 files changed, 129 insertions(+), 32 deletions(-) create mode 100644 DMS.WPF/Helper/VisualTreeFinder.cs diff --git a/DMS.WPF/Helper/VisualTreeFinder.cs b/DMS.WPF/Helper/VisualTreeFinder.cs new file mode 100644 index 0000000..c583193 --- /dev/null +++ b/DMS.WPF/Helper/VisualTreeFinder.cs @@ -0,0 +1,90 @@ + +using System.Collections.Generic; +using System.Linq; +using System.Windows; +using System.Windows.Media; + +namespace DMS.WPF.Helper +{ + public static class VisualTreeFinder + { + /// + /// (按名称查找) 在可视化树中查找指定名称的特定类型 T 的子元素。 + /// + /// 要查找的元素的类型,必须是 FrameworkElement。 + /// 要开始查找的父元素。 + /// 要查找的元素的 Name 属性。 + /// 找到的第一个匹配的元素,如果未找到则返回 null。 + public static T FindVisualChildByName(DependencyObject parent, string name) where T : FrameworkElement + { + if (parent == null || string.IsNullOrEmpty(name)) + return null; + + int childrenCount = VisualTreeHelper.GetChildrenCount(parent); + for (int i = 0; i < childrenCount; i++) + { + var child = VisualTreeHelper.GetChild(parent, i); + + // 检查当前子元素是否是我们要找的目标 + if (child is T frameworkElement && frameworkElement.Name == name) + { + return frameworkElement; + } + + // 如果不是,则递归进入子元素的子级中查找 + var result = FindVisualChildByName(child, name); + if (result != null) + { + // 如果在子级中找到了,立即返回结果,停止继续搜索 + return result; + } + } + + return null; + } + + /// + /// (非泛型) 查找指定元素的所有可视化子元素。 + /// + /// 要开始查找的父元素。 + /// 一个包含所有找到的子元素的 IEnumerable 集合。 + public static IEnumerable FindAllVisualChildren(DependencyObject parent) + { + if (parent == null) + yield break; + + int childrenCount = VisualTreeHelper.GetChildrenCount(parent); + for (int i = 0; i < childrenCount; i++) + { + DependencyObject child = VisualTreeHelper.GetChild(parent, i); + + // 先返回直接子元素 + yield return child; + + // 然后递归查找并返回其所有子元素 + foreach (DependencyObject nestedChild in FindAllVisualChildren(child)) + { + yield return nestedChild; + } + } + } + + /// + /// (泛型) 查找指定元素的可视化子元素中所有符合条件的特定类型 T 的元素。 + /// + /// 要查找的子元素的类型,必须是 DependencyObject。 + /// 要开始查找的父元素。 + /// 一个包含所有找到的子元素的 IEnumerable 集合。 + public static IEnumerable FindVisualChildren(DependencyObject parent) where T : DependencyObject + { + // 复用 FindAllVisualChildren 的逻辑,避免代码重复 + foreach (var child in FindAllVisualChildren(parent)) + { + if (child is T typedChild) + { + yield return typedChild; + } + } + } + } +} diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 47f2668..90b7652 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -19,7 +19,7 @@ namespace DMS.WPF.Services { typeof(ConfrimDialogViewModel), typeof(ConfirmDialog) }, { typeof(VariableTableDialogViewModel), typeof(VariableTableDialog) }, { typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) }, - { typeof(VariableDialogViewModel), typeof(VariableEditorDialog) }, + { typeof(VariableDialogViewModel), typeof(VariableDialog) }, // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // ... other dialogs }; diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index e860d74..238f192 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -495,19 +495,12 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 显示添加变量数据的对话框 VariableDialogViewModel variableDialogViewModel=new VariableDialogViewModel("添加变量","添加变量"); - VariableEditorDialog editorDialog = new VariableEditorDialog() { DataContext = variableDialogViewModel }; - Dialog.Show(new MessageBoxInfo() - { - Message = "操作成功", - Caption = "提示", - Button = MessageBoxButton.OKCancel, - }); - // var variableItemViewModel = await _dialogService.ShowDialogAsync(variableDialogViewModel); + var variableItemViewModel = await _dialogService.ShowDialogAsync(variableDialogViewModel); // 如果用户取消或对话框未返回数据,则直接返回 - // if (res == null) - // return; + if (variableItemViewModel == null) + return; // // 设置新变量的所属变量表ID // varData.VariableTableId = variableTable.Id; diff --git a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml index e33dc8d..6e06cba 100644 --- a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml +++ b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml @@ -39,7 +39,7 @@ - diff --git a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml.cs b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml.cs index d151ba7..a51a0f4 100644 --- a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml.cs @@ -1,25 +1,33 @@ using System.Windows; +using System.Windows.Controls; using DMS.Core.Helper; +using DMS.WPF.Helper; using DMS.WPF.ViewModels.Dialogs; +using iNKORE.UI.WPF.Helpers; using iNKORE.UI.WPF.Modern.Controls; namespace DMS.WPF.Views.Dialogs; -public partial class DeviceDialog +public partial class DeviceDialog : ContentDialog { + private const int ContentAreaMaxWidth = 1000; + private const int ContentAreaMaxHeight = 800; + public DeviceDialog() { InitializeComponent(); + this.Opened += OnOpened; + + } + + + private void OnOpened(ContentDialog sender, ContentDialogOpenedEventArgs args) + { + //޸ĶԻݵȺ߶ + var backgroundElementBorder = VisualTreeFinder.FindVisualChildByName(this, "BackgroundElement"); + backgroundElementBorder.MaxWidth = ContentAreaMaxWidth; + backgroundElementBorder.MaxWidth = ContentAreaMaxHeight; - // Log the ProtocolType value - // if (viewModel.Device != null) - // { - // NlogHelper.Info($"DeviceDialog opened. Device ProtocolType: {viewModel.Device.ProtocolType}"); - // } - // else - // { - // NlogHelper.Info("DeviceDialog opened. Device is null."); - // } } } \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/VariableDialog.xaml b/DMS.WPF/Views/Dialogs/VariableDialog.xaml index adb11ee..15abdcf 100644 --- a/DMS.WPF/Views/Dialogs/VariableDialog.xaml +++ b/DMS.WPF/Views/Dialogs/VariableDialog.xaml @@ -7,8 +7,7 @@ xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs" xmlns:ex="clr-namespace:DMS.Extensions" - xmlns:en="clr-namespace:DMS.Core.Enums" - xmlns:sockets="clr-namespace:System.Net.Sockets;assembly=System.Net.Sockets" + xmlns:en="clr-namespace:DMS.Core.Enums;assembly=DMS.Core" xmlns:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core" xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" @@ -17,7 +16,6 @@ DefaultButton="Primary" PrimaryButtonText="{Binding PrimaryButText}" Background="#fff" - d:DataContext="{d:DesignInstance vmd:VariableDialogViewModel}" mc:Ignorable="d"> @@ -38,7 +36,6 @@ - @@ -61,7 +58,7 @@ Style="{StaticResource TextBlockSubTitle}" /> + ItemsSource="{ex:EnumBindingSource {x:Type en:ProtocolType}}"> @@ -137,9 +134,5 @@ - - - - \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/VariableDialog.xaml.cs b/DMS.WPF/Views/Dialogs/VariableDialog.xaml.cs index 081ac89..5c22c32 100644 --- a/DMS.WPF/Views/Dialogs/VariableDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/VariableDialog.xaml.cs @@ -1,14 +1,27 @@ +using DMS.WPF.Helper; using DMS.WPF.ViewModels.Dialogs; +using iNKORE.UI.WPF.Modern.Controls; +using System.Windows.Controls; namespace DMS.WPF.Views.Dialogs; public partial class VariableDialog { + private const int ContentAreaMaxWidth = 1000; + private const int ContentAreaMaxHeight = 800; public VariableDialog() { InitializeComponent(); + this.Opened += OnOpened; + } + + private void OnOpened(ContentDialog sender, ContentDialogOpenedEventArgs args) + { + //޸ĶԻݵȺ߶ + var backgroundElementBorder = VisualTreeFinder.FindVisualChildByName(this, "BackgroundElement"); + backgroundElementBorder.MaxWidth = ContentAreaMaxWidth; + backgroundElementBorder.MaxWidth = ContentAreaMaxHeight; - } } \ No newline at end of file