Как получить общий тип ItemContainer для WPF ItemsControl
Я хочу определить тип ItemContainer из существующего объекта ItemsControl.
var item = control as ItemsControl;
//HOW to get child container Type?
Пример того, как blend делает это:

Blend каким-то образом определяет, что текущий TabControl тип дочернего элемента является TabItem.
Как сделать то же самое в коде?
1 ответ:
Существует
Обратите внимание, что вы должны быть осторожны, чтобы не получить атрибут из базового класса. Кроме того, в то время как это работает для большинства типов (StyleTypedPropertyAttributeо большинстве классов, производных отItemsControl. Получаем тот, у которогоPropertyравно"ItemContainerStyle". СвойствоStyleTargetTypeэтого атрибута должно дать вам тип элемента.TabControl,ListBox), некоторые классы, такие какDataGrid, просто не аннотируются этим атрибутом.Вот список, который я использую для встроенных типов фреймворков:
var _itemsContainerTypeByContainerType = new Dictionary<Type, Type> { { typeof(ComboBox), typeof(ComboBoxItem) }, { typeof(ContextMenu), typeof(MenuItem) }, { typeof(DataGrid), typeof(DataGridRow) }, { typeof(DataGridCellsPresenter), typeof(DataGridCell) }, { typeof(DataGridColumnHeadersPresenter), typeof(DataGridColumnHeader) }, { typeof(HeaderedItemsControl), typeof(ContentPresenter) }, { typeof(ItemsControl), typeof(ContentPresenter) }, { typeof(ListBox), typeof(ListBoxItem) }, { typeof(ListView), typeof(ListViewItem) }, { typeof(Menu), typeof(MenuItem) }, { typeof(MenuBase), typeof(MenuItem) }, { typeof(MenuItem), typeof(MenuItem) }, { typeof(MultiSelector), typeof(ContentPresenter) }, { typeof(Selector), typeof(ContentPresenter) }, { typeof(StatusBar), typeof(StatusBarItem) }, { typeof(TabControl), typeof(TabItem) }, { typeof(TreeView), typeof(TreeViewItem) }, { typeof(TreeViewItem), typeof(TreeViewItem) } };