WPF 查詢子元素 查詢父元素方法

2021-10-01 23:06:34 字數 3277 閱讀 1896

一、查詢子元素的方法:

#region functions.visual.child

/// /// 列舉:搜尋子元素模式

///

public enum findchildrenmode

#endregion

#region functions.visual.child

/// /// get:獲取符合型別的迭代

///

/// 查詢的型別

/// 查詢的元素

/// 搜尋子元素模式

/// 搜尋物件是否包括t元素

///

public static ienumerablegetchildrenenumerable(dependencyobject visual, findchildrenmode mode, bool issearcht = true)

where t : frameworkelement

var _count = visualtreehelper.getchildrencount(visual);

switch (mode)

if (_child is t _target)

}#endregion

}break;

case findchildrenmode.branch:

if (_child is t _target)

}var _enumerable = getchildrenenumerable(_child, mode, issearcht);

foreach (t _sub in _enumerable)

}#endregion

}break;

case findchildrenmode.layer:

if (_child is t _target)

}for (int i = 0; i < _count; i++)

if (!issearcht)

}var _enumerable = getchildrenenumerable(_child, mode, issearcht);

foreach (t _sub in _enumerable)

}#endregion

}break;}}

/// /// get:獲取符合型別的迭代

///

/// 查詢的型別

/// 查詢的元素

///

public static ienumerablegetchildrenenumerable(dependencyobject visual)

where t : frameworkelement

/// /// get:獲取所有符合型別的子元素

///

/// 查詢的型別

/// 查詢的元素

///

public static listfindallchild(dependencyobject visual)

where t : frameworkelement

var _enumerable = getchildrenenumerable(visual);

_list.addrange(_enumerable);

return _list;

}/// /// get:獲取首個符合型別的子元素

///

/// 查詢的型別

/// 查詢的元素

///

public static t firstchild(dependencyobject visual)

where t : frameworkelement

var _enumerable = getchildrenenumerable(visual);

foreach (var _child in _enumerable)

return _child;

}return default;

}/// /// get:獲取首個符合型別的子元素

///

/// 查詢的型別

/// 查詢的元素

/// 元素名稱

///

public static t firstchild(dependencyobject visual, string name)

where t : frameworkelement

var _enumerable = getchildrenenumerable(visual);

foreach (var _child in _enumerable)

if (_child.name == name)

}return default;

}#endregion

二、查詢父元素的方法:

#region functions.visual.parent

/// /// get:獲取符合型別的父元素

///

/// 父控制項的型別

/// 要找的是obj的父控制項

/// 目標父控制項

public static t getparent(dependencyobject visual)

where t : frameworkelement

var _parent = visualtreehelper.getparent(visual);

while (_parent != null)

// 在上一級父控制項中沒有找到指定名字的控制項,就再往上一級找

_parent = visualtreehelper.getparent(_parent);

}return default;

}/// /// get:獲取符合型別的父元素

///

/// 父控制項的型別

/// 要找的是obj的父控制項

/// 想找的父控制項的name屬性

/// 目標父控制項

public static t getparent(visual visual, string name)

where t : frameworkelement

var _parent = visualtreehelper.getparent(visual);

while (_parent != null)

}// 在上一級父控制項中沒有找到指定名字的控制項,就再往上一級找

_parent = visualtreehelper.getparent(_parent);

}return default;

}#endregion

jQuery 查詢父元素

function deletesec1div5 obj 自己寫的一段 實現了table中的全選,反全選,刪除功能。html 如下 學生請假明細表 新增請假資訊 搜尋 學號姓名性別 班級請假日期 銷假日期 請教事由 備註選擇 20160101 李常茹女 小一班2016.10.10 2016.10.11...

jquery查詢父元素 子元素(個人總結)

使用js或者jquery查詢父元素 子元素經常遇到。可是用起來總容易混淆,這裡統一總結了一下,以後用起來相信會方便好多 這裡jquery向上查詢父元素 用到的方法 closest parents parent 向下查詢子元素 用到的方法 find children js用的是 children 屬性...

jQuery查詢子元素與後代元素

子元素 children 方法 返回返回被選元素的所有直接子元素。children 選擇器 如選擇type為file的子元素 this children input type file 或者 p s 後代元素 find 方法 獲得當前元素集合中每個元素的後代,通過選擇器 jquery 物件或元素來篩...