TabControl-数据绑定TabItem的顺序 [英] TabControl - databinding TabItem order

查看:229
本文介绍了TabControl-数据绑定TabItem的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定的TabControl,并且想将每个TabItem的索引绑定到我的视图模型中的相应属性. ItemsSource是一个ObservableCollection,我正在使用 Bea Stollnitz的拖放功能提供选项卡控件的重新排序.

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.

推荐答案

您的TabControl.ItemsSource应该绑定到您的收藏夹,因此要重新排列选项卡项的顺序,只需重新排列收藏夹即可.

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

在创建一个TabControl允许用户拖放选项卡项目之前,我曾与Bea的拖放代码一起工作过,我认为她所提供的代码大部分是所需的.放下时,它将从其父集合中删除被拖动的对象,并将其插入到放置目标集合中的新位置,在您的情况下,该目标是同一集合.

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.

修改

根据下面有关使用Tab索引更新ViewModel的评论,请尝试使用CollectionChanged事件.

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);
}

这篇关于TabControl-数据绑定TabItem的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆