using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace ZJClassTool.Views
public class MyListBox : ListBox
protected override DependencyObject GetContainerForItemOverride()
return new MyListBoxItem();
public class MyListBoxItem : ListBoxItem
protected override void OnSelected(System.Windows.RoutedEventArgs e)
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is ListBoxItem))
dep = VisualTreeHelper.GetParent(dep);
if (dep == null) { return; }
ListBoxItem item = (ListBoxItem)dep;
if (item.IsSelected)
item.IsSelected = !item.IsSelected;
base.OnSelected(e);
}
private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
object o = toolbar_list.SelectedItem;
if (o == null)
return;
MessageBox.Show(o.ToString());
}
ItemsControl
Item中添加Button,对Button添加事件,获取Button所在Item的Index
工具类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace ZJClassTool.Utils
class VTHelper
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
var child = VisualTreeHelper.GetChild(parent, i);
// 如果子控件不是需查找的控件类型
T childType = child as T;
if (childType == null)
// 在下一级控件中递归查找
foundChild = FindChild<T>(child, childName);
// 找到控件就可以中断递归操作
if (foundChild != null) break;
else if (!string.IsNullOrEmpty(childName))
var frameworkElement = child as FrameworkElement;
// 如果控件名称符合参数条件
if (frameworkElement != null && frameworkElement.Name == childName)
foundChild = (T)child;
break;
// 查找到了控件
foundChild = (T)child;
break;
return foundChild;
public static List<T> FindChilds<T>(DependencyObject parent, string childName)
where T : DependencyObject
var list = new List<T>();
if (parent == null) return list;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
var child = VisualTreeHelper.GetChild(parent, i);
// 如果子控件不是需查找的控件类型
T childType = child as T;
if (childType == null)
// 在下一级控件中递归查找
var findChildList = FindChilds<T>(child, childName);
for (int j = 0; j < findChildList.Count; j++)
list.AddRange(FindChilds<T>(child, childName));
else if (!string.IsNullOrEmpty(childName))
var frameworkElement = child as FrameworkElement;
// 如果控件名称符合参数条件
if (frameworkElement != null && frameworkElement.Name == childName)
list.Add((T)child);
// 查找到了控件
list.Add((T)child);
return list;
/// <summary>
/// 查找父元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="name"></param>
/// <returns></returns>
public static T FindParent<T>(DependencyObject i_dp) where T : DependencyObject
DependencyObject dobj = VisualTreeHelper.GetParent(i_dp);
if (dobj != null)
if (dobj is T)
return (T)dobj;
dobj = FindParent<T>(dobj);
if (dobj != null && dobj is T)
return (T)dobj;
return null;
}