How to change the order of the TabItem in the wpf TabControl

泄露秘密 提交于 2019-12-07 08:57:02

问题


I need to change the order of the TabItem.

I've tried with Remove / Insert and it doesn't works.

void UserControl_Loaded(object sender, RoutedEventArgs e) {
  if(condition) {
    TabControl.Items.Remove(TabItem);
      TabControl.Items.Insert(0, TabItem);
    }
}

InvalidOperationException: Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.

How to solve this?


回答1:


Solved using the "for" instead of "foreach".

if(condition) {    
  var tabItem = Tab.Items[index];
  Tab.Items.RemoveAt(index);
  Tab.Items.Insert(0, tabItem);
  ((TabItem)tabItem).IsSelected = true;
}


来源:https://stackoverflow.com/questions/4043179/how-to-change-the-order-of-the-tabitem-in-the-wpf-tabcontrol

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