潜移默化学会WPF(技巧篇)--具有Items元素的控件子项获取(一)
1. treeview的Item获取
var g = this.tree.ItemContainerGenerator; TreeViewItem item = g.ContainerFromItem(this.tree.SelectedItem) as TreeViewItem; if (item != null) item.IsExpanded = true;
2.ListBox
var g = listbox实例.ItemContainerGenerator; ListBoxItem item = g.ContainerFromIndex(listbox实例.SelectedIndex) as ListBoxItem
3.Combobox
(comboBox1.ItemContainerGenerator.ContainerFromIndex(comboBox1.SelectedIndex) as ComboBoxItem).Content.ToString()