using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Media;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Security.Cryptography;
|
|
namespace XHandler.Class
|
{
|
/// <summary>
|
/// 工具类
|
/// </summary>
|
public class Utilities
|
{
|
/// <summary>
|
/// 获取父元素
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static T FindVisualParent<T>(DependencyObject obj) where T : class
|
{
|
while (obj != null)
|
{
|
if (obj is T)
|
{
|
return obj as T;
|
}
|
|
obj = VisualTreeHelper.GetParent(obj);
|
}
|
|
return null;
|
}
|
|
/// <summary>
|
/// 获取第一个子控件
|
/// </summary>
|
/// <typeparam name="ChildType"></typeparam>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
|
{
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
|
{
|
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
|
if (child != null && child is ChildType)
|
{
|
return child as ChildType;
|
}
|
else
|
{
|
ChildType childOfChildren = FindVisualChild<ChildType>(child);
|
if (childOfChildren != null)
|
{
|
return childOfChildren;
|
}
|
}
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 获取指定类型的所有子控件
|
/// </summary>
|
/// <typeparam name="ChildType"></typeparam>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static List<ChildType> FindAllVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
|
{
|
List<ChildType > children = new List < ChildType >();
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
|
{
|
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
|
if (child != null && child is ChildType)
|
{
|
children.Add(child as ChildType);
|
}
|
else
|
{
|
List<ChildType> childOfChildren = FindAllVisualChild<ChildType>(child);
|
if (childOfChildren != null&& childOfChildren.Count>0)
|
{
|
children.AddRange(childOfChildren);
|
}
|
}
|
}
|
return children;
|
}
|
|
/// <summary>
|
/// 获取最后一个子控件
|
/// </summary>
|
/// <typeparam name="ChildType"></typeparam>
|
/// <param name="obj"></param>
|
/// <returns></returns>
|
public static ChildType FindLastVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
|
{
|
for (int i = VisualTreeHelper.GetChildrenCount(obj)-1; i >=0; i--)
|
{
|
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
|
if (child != null && child is ChildType)
|
{
|
return child as ChildType;
|
}
|
else
|
{
|
ChildType childOfChildren = FindLastVisualChild<ChildType>(child);
|
if (childOfChildren != null)
|
{
|
return childOfChildren;
|
}
|
}
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 移除元素
|
/// </summary>
|
/// <param name="element"></param>
|
public static void RemoveChild(UIElement element)
|
{
|
Window wnd = Application.Current.MainWindow;
|
Grid grid = Utilities.FindVisualChild<Grid>(wnd);
|
grid.Children.Remove(element);
|
}
|
|
|
public static TreeViewItem GetDataFromTreeViewItem(TreeView source, Point point)
|
{
|
UIElement element = source.InputHitTest(point) as UIElement; //直接获取最底层的元素,这里是textblock
|
if (element != null)
|
{
|
return FindVisualParent<TreeViewItem>(element);//获取父控件
|
}
|
return null;
|
}
|
|
public static TreeViewItem FindTreeViewItem(ItemsControl item, object data)
|
{
|
TreeViewItem findItem = null;
|
bool itemIsExpand = false;
|
if (item is TreeViewItem)
|
{
|
TreeViewItem tviCurrent = item as TreeViewItem;
|
itemIsExpand = tviCurrent.IsExpanded;
|
if (!tviCurrent.IsExpanded)
|
{
|
//如果这个TreeViewItem未展开过,则不能通过ItemContainerGenerator来获得TreeViewItem
|
tviCurrent.SetValue(TreeViewItem.IsExpandedProperty, true);
|
//必须使用UpdaeLayour才能获取到TreeViewItem
|
tviCurrent.UpdateLayout();
|
}
|
}
|
for (int i = 0; i < item.Items.Count; i++)
|
{
|
TreeViewItem tvItem = (TreeViewItem)item.ItemContainerGenerator.ContainerFromIndex(i);
|
if (tvItem == null)
|
continue;
|
object itemData = item.Items[i];
|
if (itemData == data)
|
{
|
findItem = tvItem;
|
break;
|
}
|
else if (tvItem.Items.Count > 0)
|
{
|
findItem = FindTreeViewItem(tvItem, data);
|
if (findItem != null)
|
break;
|
}
|
}
|
if (findItem == null)
|
{
|
TreeViewItem tviCurrent = item as TreeViewItem;
|
tviCurrent.SetValue(TreeViewItem.IsExpandedProperty, itemIsExpand);
|
tviCurrent.UpdateLayout();
|
}
|
|
return findItem;
|
}
|
|
public static bool isNumberic(string _string)
|
{
|
if (string.IsNullOrEmpty(_string))
|
return false;
|
foreach (char c in _string)
|
{
|
if (!char.IsDigit(c))
|
//if(c<'0' || c>'9')//最好的方法,在下面测试数据中再加一个0,然后这种方法效率会搞10毫秒左右
|
return false;
|
}
|
return true;
|
}
|
|
public static string GetMD5(string password)
|
{
|
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
|
byte[] bytValue, bytHash;
|
bytValue = System.Text.Encoding.UTF8.GetBytes(password);
|
bytHash = md5.ComputeHash(bytValue);
|
md5.Clear();
|
string sTemp = "";
|
for (int i = 0; i < bytHash.Length; i++)
|
{
|
sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
|
}
|
return sTemp.ToLower();
|
}
|
|
/// <summary>
|
/// 查询通道在通道组中的下标值
|
/// </summary>
|
/// <param name="chArray"></param>
|
/// <param name="chNum"></param>
|
/// <returns></returns>
|
public static int GetArrayIndex(int[] chArray,int chNum)
|
{
|
int index = 0;
|
for(int i=0;i< chArray.Length;i++)
|
{
|
if (chArray[i]==chNum)
|
{
|
index = i; break;
|
}
|
}
|
return index;
|
}
|
|
public static float[] GetFloatArrayFromStringArray(string[] strArray)
|
{
|
float[] floatArray= new float[strArray.Length];
|
for(int i=0;i<strArray.Length;i++)
|
{
|
floatArray[i] = (float)Convert.ToDouble(strArray[i]);
|
}
|
return floatArray;
|
}
|
}
|
}
|