wpfxamllistviewitemitemspresenter

Inherited from an itemsControl - How to change the type of the items?


I'm trying to improve ListView. This mostly has to do with changed in the ListViewItem class. So I inherited from both, creating two costume controls:

NestedListView : ListView 
NestedListViewItem : ListViewItem

The problem is that now I want the NestedListView's <ItemsPresenter/> to present NestedListViewItems instead of regular ListViewItems. How do I achieve this?

(I pretty much have no idea how <ItemsPresenter/> works. So any explanation on that area could be helpful).


Solution

  • Override the GetContainerForItemOverride method and return the type of item container you want:

    public class NestedListView : ListView
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new NestedListViewItem();
        }
    
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is NestedListViewItem;
        }
    }