6

ItemContainer既存のItemsControlオブジェクトから型を判別したい。

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

Blend でそれがどのように行われるかの例:

ここに画像の説明を入力

Blend はどういうわけか、現在のTabControlタイプの子項目がであると判断しますTabItem

コードで同じことを行う方法は?

4

1 に答える 1

8

StyleTypedPropertyAttributeから派生したほとんどのクラスにがありItemsControlます。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) }
};
于 2012-09-15T11:44:57.333 に答える