This commit is contained in:
Meik
2026-03-05 09:56:57 +01:00
parent 838e6b1ee1
commit 4013fa8e32
827 changed files with 743038 additions and 0 deletions

94
libs/visualHelper.cs Normal file
View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Windows;
namespace C4IT_CustomerPanel.libs
{
class visualHelper
{
public static List<T> GetLogicalChildCollection<T>(object parent) where T : DependencyObject
{
List<T> logicalCollection = new List<T>();
GetLogicalChildCollection(parent as DependencyObject, logicalCollection);
return logicalCollection;
}
private static void GetLogicalChildCollection<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
{
System.Collections.IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildCollection(depChild, logicalCollection);
}
}
}
public static T FindVisualChild<T>(DependencyObject parent, string Tag = null) where T : DependencyObject
{
try
{
var Cnt = System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < Cnt ; i++)
{
DependencyObject child = System.Windows.Media.VisualTreeHelper.GetChild(parent, i);
string _Tag = (child as FrameworkElement)?.Tag as string;
if (child is T visualChild && (Tag == null || _Tag == Tag))
{
return visualChild;
}
else
{
T childOfChild = FindVisualChild<T>(child, Tag);
if (childOfChild != null)
return childOfChild;
}
}
}
catch (Exception)
{
}
return null;
}
public static Window GetMainWindow()
{
return Application.Current.MainWindow;
}
public static Window GetUserControlOwnerWindow(System.Windows.Forms.Control usercontrol)
{
return GetUserControlOwnerWindow(usercontrol.Parent);
}
// ******************************************************************
public static Window GetUserControlOwnerWindow(DependencyObject parent)
{
while (parent != null && !(parent is Window))
{
parent = LogicalTreeHelper.GetParent(parent);
}
return parent as Window;
}
}
}