TabControl - databinding TabItem order

我们两清 提交于 2019-12-25 08:20:12

问题


I've got a datababound TabControl and would like to bind the index of each TabItem to a corresponding property in my view model. The ItemsSource is an ObservableCollection, and I'm using Bea Stollnitz's Drag/Drop functionality to provide tab control re-ordering.

My gut feeling is that it should be able to be handled in the data template for the tab item header, but I haven't been able to get it working.


回答1:


Your TabControl.ItemsSource should be bound to your collection, so to re-arrange the order of tab items, simply re-arrange the collection.

I've worked with Bea's drag/drop code before to create a TabControl that allowed users to drag/drop the tab items, and I think most of what was needed is in the code she provides. On drop, it removes the dragged object from it's parent collection, and inserts it to its new location in the drop target collection, which in your case is the same collection.

Edit

Based on your comment below about updating your ViewModel with the Tab Index, try using the CollectionChanged event.

void MyCollection_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
    foreach (var item in MyCollection)
        item.TabIndex = MyCollection.IndexOf(item);
}


来源:https://stackoverflow.com/questions/9877125/tabcontrol-databinding-tabitem-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!