WPF - Best way to remove an item from the ItemsSource

前端 未结 4 546
日久生厌
日久生厌 2020-12-17 02:22

I\'m writing a custom ItemsControl (a tabbed document container), where each item (tab) can remove itself from the UI when the user closes it. However, I can\'t

4条回答
  •  猫巷女王i
    2020-12-17 02:54

    The ItemCollection returned by ItemsControl.Items won't allow you to call Remove directly, but it implements IEditableCollectionView and does allow you to call the Remove method in that interface.

    This will only work if the collection view bound to ItemsSource implements IEditableCollectionView itself. The default collection view will for most mutable collections, although not for objects that implement ICollection but not IList.

    IEditableCollectionView items = tabControl.Items; //Cast to interface
    if (items.CanRemove)
    {
        items.Remove(tabControl.SelectedItem);
    }
    

提交回复
热议问题