Question

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?

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top