整理控件库

This commit is contained in:
GG Z
2025-05-05 17:04:06 +08:00
parent 74532b77be
commit 3eaad7566e
283 changed files with 2156 additions and 17846 deletions

View File

@@ -9,17 +9,17 @@ namespace WPFluent.Extensions
public static T FindEqualElement<T>(DependencyObject source, DependencyObject element)
where T : DependencyObject
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++)
{
var child = VisualTreeHelper.GetChild(source, i);
if(child != null && child is T && child == element)
if (child != null && child is T t && child == element)
{
return (T)child;
return t;
}
else
{
var childOfChild = FindVisualChild<T>(child);
if(childOfChild != null)
if (childOfChild != null)
{
return childOfChild;
}
@@ -37,16 +37,16 @@ namespace WPFluent.Extensions
public static T FindParent<T>(this DependencyObject i_dp) where T : DependencyObject
{
var dobj = VisualTreeHelper.GetParent(i_dp);
if(dobj != null)
if (dobj != null)
{
if(dobj is T t)
if (dobj is T t)
{
return t;
}
else
{
dobj = FindParent<T>(dobj);
if(dobj is not null and T)
if (dobj is not null and T)
{
return (T)dobj;
}
@@ -58,16 +58,16 @@ namespace WPFluent.Extensions
public static T FindParent<T>(this DependencyObject i_dp, string elementName) where T : DependencyObject
{
var dobj = VisualTreeHelper.GetParent(i_dp);
if(dobj != null)
if (dobj != null)
{
if(dobj is T && ((System.Windows.FrameworkElement)(dobj)).Name.Equals(elementName))
if (dobj is T && ((System.Windows.FrameworkElement)(dobj)).Name.Equals(elementName))
{
return (T)dobj;
}
else
{
dobj = FindParent<T>(dobj);
if(dobj is not null and T)
if (dobj is not null and T)
{
return (T)dobj;
}
@@ -84,15 +84,15 @@ namespace WPFluent.Extensions
/// <returns></returns>
public static T FindVisualChild<T>(this DependencyObject obj) where T : DependencyObject
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if(child is not null and T)
if (child is not null and T)
return (T)child;
else
{
var childOfChild = FindVisualChild<T>(child);
if(childOfChild != null)
if (childOfChild != null)
return childOfChild;
}
}
@@ -107,17 +107,17 @@ namespace WPFluent.Extensions
/// <returns></returns>
public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
{
if(depObj != null)
if (depObj != null)
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if(child is not null and T)
if (child is not null and T)
{
yield return (T)child;
}
foreach(var childOfChild in FindVisualChildren<T>(child))
foreach (var childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
@@ -136,14 +136,14 @@ namespace WPFluent.Extensions
try
{
List<T> TList = [];
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if(child is not null and T)
if (child is not null and T)
{
TList.Add((T)child);
var childOfChildren = FindVisualChildrenEx<T>(child);
if(childOfChildren != null)
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
@@ -151,14 +151,15 @@ namespace WPFluent.Extensions
else
{
var childOfChildren = FindVisualChildrenEx<T>(child);
if(childOfChildren != null)
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
}
}
return TList;
} catch
}
catch
{
return null;
}
@@ -174,21 +175,21 @@ namespace WPFluent.Extensions
public static childItem FindVisualElement<childItem>(this DependencyObject obj, string elementName)
where childItem : DependencyObject
{
for(var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if(child != null &&
if (child != null &&
child is childItem &&
((System.Windows.FrameworkElement)(child)).Name.Equals(elementName))
return (childItem)child;
else
{
IEnumerator j = FindVisualChildren<childItem>(child).GetEnumerator();
while(j.MoveNext())
while (j.MoveNext())
{
var childOfChild = (childItem)j.Current;
if(childOfChild != null && !(childOfChild as FrameworkElement).Name.Equals(elementName))
if (childOfChild != null && !(childOfChild as FrameworkElement).Name.Equals(elementName))
{
FindVisualElement<childItem>(childOfChild, elementName);
}
@@ -212,11 +213,11 @@ namespace WPFluent.Extensions
{
DependencyProperty prop = null;
if(type != null)
if (type != null)
{
var fieldInfo = type.GetField($"{propertyName}Property", BindingFlags.Static | BindingFlags.Public);
if(fieldInfo != null)
if (fieldInfo != null)
{
prop = fieldInfo.GetValue(null) as DependencyProperty;
}
@@ -235,7 +236,7 @@ namespace WPFluent.Extensions
{
DependencyProperty prop = null;
if(o != null)
if (o != null)
{
prop = GetDependencyProperty(o.GetType(), propertyName);
}
@@ -251,33 +252,35 @@ namespace WPFluent.Extensions
/// </summary>
public static object GetPropertyValue(this object obj, string path)
{
if(obj == null)
if (obj == null)
return string.Empty;
var flag = !string.IsNullOrEmpty(path);
object result;
if(flag)
if (flag)
{
var property = obj.GetType().GetProperty(path);
var flag2 = property != null;
if(flag2)
if (flag2)
{
try
{
result = property.GetValue(obj, null);
return result;
} catch
}
catch
{
try
{
var type = obj.GetType();
var mInfo = type.GetMethod($"get_{path}");
if(mInfo != null)
if (mInfo != null)
{
result = mInfo.Invoke(obj, null);
return result;
}
} catch
}
catch
{
}
}
@@ -306,18 +309,18 @@ namespace WPFluent.Extensions
/// <returns></returns>
public static bool SetIfDefault<T>(this DependencyObject o, DependencyProperty property, T value)
{
if(o == null)
if (o == null)
throw new ArgumentNullException(nameof(o), "DependencyObject cannot be null");
if(property == null)
if (property == null)
throw new ArgumentNullException(nameof(property), "DependencyProperty cannot be null");
if(!property.PropertyType.IsAssignableFrom(typeof(T)))
if (!property.PropertyType.IsAssignableFrom(typeof(T)))
{
throw new ArgumentException(
$"Expected {property.Name} to be of type {typeof(T).Name} but was {property.PropertyType}");
}
if(DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
if (DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
{
o.SetValue(property, value);