准备继续优化

This commit is contained in:
zengwenjie
2025-09-30 10:07:48 +08:00
parent 20e5b0c33f
commit 3c0e12ae9a
2 changed files with 29 additions and 7 deletions

View File

@@ -60,7 +60,7 @@ namespace Deedy.Activity
/// <typeparam name="T">要查找的祖先元素类型</typeparam> /// <typeparam name="T">要查找的祖先元素类型</typeparam>
/// <param name="object">起始子元素</param> /// <param name="object">起始子元素</param>
/// <param name="level">要查找的祖先层级(1=父级2=祖父级等)</param> /// <param name="level">要查找的祖先层级(1=父级2=祖父级等)</param>
/// <returns>找到的祖先元素若未找到则返回null</returns> /// <returns>找到的祖先元素,若未找到则返回null</returns>
public static T? FindAncestor<T>(this DependencyObject @object, int level = 1) where T : DependencyObject public static T? FindAncestor<T>(this DependencyObject @object, int level = 1) where T : DependencyObject
{ {
if (@object == null || level < 1) return null; if (@object == null || level < 1) return null;
@@ -82,5 +82,27 @@ namespace Deedy.Activity
return null; return null;
} }
/// <summary>
/// 在可视化树中查找指定类型的子级元素,支持元素名称
/// </summary>
/// <typeparam name="T">要查找的子级元素类型</typeparam>
/// <param name="parent">起始父元素</param>
/// <param name="name">要查找的子级元素名称只对「FrameworkElement」类型元素生效</param>
/// <returns>找到的子级元素若未找到则返回「null」</returns>
public static T? FindVisualChild<T>(this DependencyObject parent, string name = "") where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T result)
{
if (string.IsNullOrEmpty(name)) return result;
else if (result is FrameworkElement element && element.Name == name) return result;
}
var descendant = child.FindVisualChild<T>(name);
if (descendant != null) return descendant;
}
return null;
}
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Markup; using System.Windows.Markup;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shell; using Shell = System.Windows.Shell;
namespace Deedy namespace Deedy
{ {
@@ -84,7 +84,7 @@ namespace Deedy
private Menu? MainMenu; private Menu? MainMenu;
private Window? Target; private Window? Target;
private WindowChrome? Chrome; private Shell.WindowChrome? Chrome;
private static bool IsNeedOverrideMetadata = true; private static bool IsNeedOverrideMetadata = true;
public WindowHeader() { } public WindowHeader() { }
@@ -140,10 +140,10 @@ namespace Deedy
if (this.Background == BackgroundProperty.DefaultMetadata.DefaultValue) this.SetBinding(BackgroundProperty, new Binding() { Source = this.Target, Path = new PropertyPath(BackgroundProperty.Name) }); if (this.Background == BackgroundProperty.DefaultMetadata.DefaultValue) this.SetBinding(BackgroundProperty, new Binding() { Source = this.Target, Path = new PropertyPath(BackgroundProperty.Name) });
if (this.BorderBrush == BorderBrushProperty.DefaultMetadata.DefaultValue) this.SetBinding(BorderBrushProperty, new Binding() { Source = this.Target, Path = new PropertyPath(BorderBrushProperty.Name) }); if (this.BorderBrush == BorderBrushProperty.DefaultMetadata.DefaultValue) this.SetBinding(BorderBrushProperty, new Binding() { Source = this.Target, Path = new PropertyPath(BorderBrushProperty.Name) });
//这是第【3】步 //这是第【3】步
this.Chrome = new WindowChrome() { CaptionHeight = this.ActualHeight }; this.Chrome = new Shell.WindowChrome() { CaptionHeight = this.ActualHeight };
WindowChrome.SetWindowChrome(this.Target, this.Chrome); Shell.WindowChrome.SetWindowChrome(this.Target, this.Chrome);
WindowChrome.SetIsHitTestVisibleInChrome(this.Container, true); Shell.WindowChrome.SetIsHitTestVisibleInChrome(this.Container, true);
WindowChrome.SetIsHitTestVisibleInChrome(this.Controller, true); Shell.WindowChrome.SetIsHitTestVisibleInChrome(this.Controller, true);
if (this.Controller != null) if (this.Controller != null)
{ {