2012-01-11 62 views

回答

1

您需要更改TabControl.Items集合。從舊位置移除標籤並將其設置在新的Positon上。

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

+0

在該示例中,爲什麼再次插入0索引而不考慮刪除索引? – nag 2012-01-12 04:51:57

+0

它給了我錯誤'元素已經有了一個邏輯父項。它必須與舊的父母分離後才能與新的父母連接。「 – nag 2012-01-12 05:03:52

2

如果您正在使用ObservableCollection的,你只需要改變你的集合中的項目將在視圖中refelected的位置...

對於舉例..

<TabControl ContentTemplate="{StaticResource ResourceKey=listView}" 
      ItemContainerStyle="{StaticResource ResourceKey=myTabItem}" 
      ItemsSource="{Binding Path=Persons}" 
      SelectedItem="{Binding Path=SelectedPerson}" 
      Style="{StaticResource ResourceKey=myTab}" 
      TabStripPlacement="Bottom"> 
    <TabControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Image Width="16" 
         Height="16" 
         Margin="0,0,2,0" 
         Source="Themes\Water lilies.jpg" /> 
       <TextBlock Margin="0,4,0,0" 
          VerticalAlignment="Center" 
          FontWeight="Bold" 
          Text="{Binding Path=Name}" /> 
      </StackPanel> 
     </DataTemplate> 
    </TabControl.ItemTemplate> 
</TabControl> 
<Button Grid.Row="1" Width="50" Command="{Binding Path=ChangePositionCommand}"> ClickMe </Button> 

這裏您只需在ViewModel中更改TabList中物品的位置,並且該位置將相應地更改...

在您的視圖模型

我有落實,如果得到的數據和設置的命令......這取決於你如何做到這一點

public ICommand ChangePositionCommand { get; private set; } 

public Person SelectedPerson 
{ 
    get { return selectedPerson; } 
    set 
    { 
     selectedPerson = value; 
     InvokePropertyChanged(new PropertyChangedEventArgs("SelectedPerson")); 
    } 
} 

private void ChangePosition(object obj) 
{ 
    int index = Persons.IndexOf(SelectedPerson); 

    if (index <= (Persons.Count-1)) 
    { 
     Persons.Move(index,index+1); 
    } 
    else 
    { 
     Persons.Move(index,0); 
    } 
} 

上面的代碼我給INdex out of bound但我沒有在附近一個IDE如此無法測試,你可以根據你的想法重新獲得它。

+0

你能否給我提供任何示例代碼片段。 – nag 2012-01-12 04:42:50

+0

@nag請參閱編輯答案 – Ankesh 2012-01-12 05:04:41

2

使用以下解決方案:

TabItem tempTab = new TabItem(); 
      tempTab = control.Items[0] as TabItem; 
      control.Items[0] = control.Items[1]; 
      control.Items[1] = tempTab; 

這肯定會工作,你必須從代碼做後面。