查找ComboBoxItem
ComboBoxItem cbi = (ComboBoxItem)(cb.ItemContainerGenerator.ContainerFromIndex(0));
根据ComboBoxItem查找ComboBox
ComboBoxItem与ComboBox在不同的VisualTree上,无法通过
VisualTreeHelper.FindParent
找到。
可以通过以下代码找到:
var comboBox = ItemsControl.ItemsControlFromItemContainer(cbi) as ComboBox;
原理可以参见官方的源码(附在了下面)。
///<summary>
/// Return the ItemsControl that owns the given container element
///</summary>
public