修改ContextDialog对话框内容区域的MaxWidth和MaxHeight
This commit is contained in:
90
DMS.WPF/Helper/VisualTreeFinder.cs
Normal file
90
DMS.WPF/Helper/VisualTreeFinder.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// (按名称查找) 在可视化树中查找指定名称的特定类型 T 的子元素。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要查找的元素的类型,必须是 FrameworkElement。</typeparam>
|
||||
/// <param name="parent">要开始查找的父元素。</param>
|
||||
/// <param name="name">要查找的元素的 Name 属性。</param>
|
||||
/// <returns>找到的第一个匹配的元素,如果未找到则返回 null。</returns>
|
||||
public static T FindVisualChildByName<T>(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<T>(child, name);
|
||||
if (result != null)
|
||||
{
|
||||
// 如果在子级中找到了,立即返回结果,停止继续搜索
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (非泛型) 查找指定元素的所有可视化子元素。
|
||||
/// </summary>
|
||||
/// <param name="parent">要开始查找的父元素。</param>
|
||||
/// <returns>一个包含所有找到的子元素的 IEnumerable<DependencyObject> 集合。</returns>
|
||||
public static IEnumerable<DependencyObject> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (泛型) 查找指定元素的可视化子元素中所有符合条件的特定类型 T 的元素。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要查找的子元素的类型,必须是 DependencyObject。</typeparam>
|
||||
/// <param name="parent">要开始查找的父元素。</param>
|
||||
/// <returns>一个包含所有找到的子元素的 IEnumerable<T> 集合。</returns>
|
||||
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject parent) where T : DependencyObject
|
||||
{
|
||||
// 复用 FindAllVisualChildren 的逻辑,避免代码重复
|
||||
foreach (var child in FindAllVisualChildren(parent))
|
||||
{
|
||||
if (child is T typedChild)
|
||||
{
|
||||
yield return typedChild;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
|
||||
</ui:ContentDialog.Resources>
|
||||
|
||||
<Grid Width="480"
|
||||
<Grid
|
||||
Margin="10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="1*" />
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
//<2F>ĶԻ<C4B6><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD>
|
||||
var backgroundElementBorder = VisualTreeFinder.FindVisualChildByName<Border>(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.");
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 @@
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<!-- 基本信息 -->
|
||||
@@ -61,7 +58,7 @@
|
||||
Style="{StaticResource TextBlockSubTitle}" />
|
||||
<ComboBox SelectedValue="{Binding Variable.Protocol, UpdateSourceTrigger=PropertyChanged}"
|
||||
SelectedValuePath="Tag"
|
||||
ItemsSource="{ex:EnumBindingSource {x:Type sockets:ProtocolType}}">
|
||||
ItemsSource="{ex:EnumBindingSource {x:Type en:ProtocolType}}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
|
||||
@@ -137,9 +134,5 @@
|
||||
</GroupBox>
|
||||
</ikw:SimpleStackPanel>
|
||||
|
||||
<ikw:SimpleStackPanel Grid.Row="0" Grid.Column="2" Width="100">
|
||||
|
||||
</ikw:SimpleStackPanel>
|
||||
|
||||
</Grid>
|
||||
</ui:ContentDialog>
|
||||
@@ -1,13 +1,26 @@
|
||||
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)
|
||||
{
|
||||
//<2F>ĶԻ<C4B6><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD>
|
||||
var backgroundElementBorder = VisualTreeFinder.FindVisualChildByName<Border>(this, "BackgroundElement");
|
||||
backgroundElementBorder.MaxWidth = ContentAreaMaxWidth;
|
||||
backgroundElementBorder.MaxWidth = ContentAreaMaxHeight;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user