问题
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