2012-09-15 48 views
5

我想从现有ItemsControl对象确定ItemContainer类型。如何获得一般ItemContainer类型WPF ItemsControl的

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

实施例共混物如何做到这一点:

enter image description here

共混物以某种方式确定当前TabControl类型的子项是TabItem

如何做同样的事情在代码?

回答

8

有从ItemsControl派生大多数类一StyleTypedPropertyAttribute。获得Property等于"ItemContainerStyle"。该属性上的StyleTargetType属性应该为您提供项目类型。

请注意,你必须要小心不要让从基类属性。此外,虽然这适用于大多数类型(TabControlListBox),一些类如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) } 
};