2012-09-15 17 views
5

Quiero determinar el tipo ItemContainer de un objeto existente ItemsControl.Cómo obtener el tipo general de ItemContainer para WPF ItemsControl

var item = control as ItemsControl; 
    //HOW to get child container Type? 

Ejemplo de cómo mezcla hace esto:

enter image description here

Mezcla de alguna manera determina que la corriente elemento secundario TabControl tipo es TabItem.

¿Cómo hacer lo mismo en el código?

Respuesta

8

Hay un StyleTypedPropertyAttribute en la mayoría de las clases derivadas de ItemsControl. Obtenga el que tiene Property igual a "ItemContainerStyle". La propiedad StyleTargetType en este atributo debería darle el tipo de elemento.

Tenga en cuenta que debe tener cuidado de no obtener atributos de la clase base. Además, aunque esto funciona para la mayoría de los tipos (TabControl, ListBox), algunas clases como DataGrid simplemente no se anotan con este atributo.

Aquí está la lista que utilizo para tipos de estructuras integradas:

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) } 
}; 
Cuestiones relacionadas