You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.8 KiB
150 lines
4.8 KiB
using Avalonia.Controls;
|
|
using Material.Dialog;
|
|
using Material.Dialog.Icons;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JiLinApp.Core.Avalonia;
|
|
|
|
public static class ControlsUtil
|
|
{
|
|
/// <summary>
|
|
/// 查找子控件
|
|
/// </summary>
|
|
/// <typeparam name="T">控件类型</typeparam>
|
|
/// <param name="parent">父控件依赖对象</param>
|
|
/// <param name="list">子控件列表</param>
|
|
public static void FindChild<T>(Control parent, ref List<T> list) where T : Control
|
|
{
|
|
if (parent == null) return;
|
|
if (parent is Panel pParent)
|
|
{
|
|
foreach (Control item in pParent.Children)
|
|
{
|
|
if (item is Panel panel) FindChild(panel, ref list);
|
|
else if (item is T child) list.Add(child);
|
|
}
|
|
}
|
|
else if (parent is Decorator dParent)
|
|
{
|
|
if (dParent.Child != null) FindChild(dParent.Child, ref list);
|
|
}
|
|
}
|
|
|
|
public static Window? GetWindow(Control control)
|
|
{
|
|
Control? temp = control;
|
|
while (temp?.Parent != null)
|
|
{
|
|
if (temp.Parent is Window w) return w;
|
|
else temp = temp.Parent is Control c ? c : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static bool TryGetWindow(Control control, out Window? window)
|
|
{
|
|
Control? temp = control;
|
|
window = null;
|
|
while (temp?.Parent != null)
|
|
{
|
|
if (temp.Parent is Window w) { window = w; break; }
|
|
else temp = temp.Parent is Control c ? c : null;
|
|
}
|
|
return window != null;
|
|
}
|
|
|
|
public static string GetText(this TextBox tb)
|
|
{
|
|
return tb.Text ?? string.Empty;
|
|
}
|
|
|
|
public static bool GetText(this TextBox tb, out string? text)
|
|
{
|
|
text = tb.Text;
|
|
return text != null;
|
|
}
|
|
|
|
public static string GetText(this ComboBox cbx)
|
|
{
|
|
string? val = string.Empty;
|
|
var item = cbx.SelectedItem;
|
|
if (item == null) return val;
|
|
if (item is Control)
|
|
{
|
|
if (TryGetValue(item, "Content", out object? content)) val = content?.ToString();
|
|
else if (TryGetValue(item, "Text", out string? text)) val = text;
|
|
}
|
|
else val = item.ToString();
|
|
return val ?? string.Empty;
|
|
}
|
|
|
|
public static bool GetText(this ComboBox cbx, out string? val)
|
|
{
|
|
var item = cbx.SelectedItem;
|
|
if (item is Control)
|
|
{
|
|
if (TryGetValue(item, "Content", out object? content)) val = content?.ToString();
|
|
else if (TryGetValue(item, "Text", out string? text)) val = text;
|
|
else val = null;
|
|
}
|
|
else val = item?.ToString();
|
|
return val != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 利用反射来判断对象是否包含某个属性
|
|
/// </summary>
|
|
/// <param name="instance">object</param>
|
|
/// <param name="propertyName">需要判断的属性</param>
|
|
/// <returns>是否包含</returns>
|
|
private static bool ContainProperty(object instance, string propertyName)
|
|
{
|
|
if (instance == null || string.IsNullOrEmpty(propertyName)) return false;
|
|
PropertyInfo? property = instance.GetType()?.GetProperty(propertyName);
|
|
return property != null;
|
|
}
|
|
|
|
private static bool TryGetValue<T>(object instance, string propertyName, out T? val)
|
|
{
|
|
if (instance == null || string.IsNullOrEmpty(propertyName))
|
|
{
|
|
val = default;
|
|
return false;
|
|
}
|
|
PropertyInfo? property = instance.GetType()?.GetProperty(propertyName);
|
|
val = (T?)property?.GetValue(instance, null);
|
|
return val != null;
|
|
}
|
|
|
|
public static async Task<DialogResult> ShowDialog(Window? window, string title = "Info", string message = "")
|
|
{
|
|
if (window == null) return DialogResult.NoResult;
|
|
var dialog = DialogHelper.CreateAlertDialog(new AlertDialogBuilderParams
|
|
{
|
|
Borderless = true,
|
|
Width = 300,
|
|
DialogHeaderIcon = DialogIconKind.Info,
|
|
ContentHeader = title,
|
|
SupportingText = message,
|
|
StartupLocation = WindowStartupLocation.CenterOwner,
|
|
});
|
|
return await dialog.ShowDialog(window);
|
|
}
|
|
|
|
public static async Task<DialogResult> ShowWarnDialog(Window? window, string title = "Warning", string message = "")
|
|
{
|
|
if (window == null) return DialogResult.NoResult;
|
|
var dialog = DialogHelper.CreateAlertDialog(new AlertDialogBuilderParams
|
|
{
|
|
Borderless = true,
|
|
Width = 300,
|
|
DialogHeaderIcon = DialogIconKind.Warning,
|
|
ContentHeader = title,
|
|
SupportingText = message,
|
|
StartupLocation = WindowStartupLocation.CenterOwner,
|
|
});
|
|
return await dialog.ShowDialog(window);
|
|
}
|
|
}
|