开发者

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

开发者 https://www.devze.com 2023-01-22 14:43 出处:网络
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) {

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 th开发者_开发问答is?


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

精彩评论

暂无评论...
验证码 换一张
取 消