我在的,我需要在運行時不斷移動的每個標籤的位置。任何人都可以告訴我如何在運行時將tab索引從一個位置更改爲另一個位置。如何在運行時更改TabControl的TabItem索引?
感謝,
@nagaraju。
我在的,我需要在運行時不斷移動的每個標籤的位置。任何人都可以告訴我如何在運行時將tab索引從一個位置更改爲另一個位置。如何在運行時更改TabControl的TabItem索引?
感謝,
@nagaraju。
您需要更改TabControl.Items
集合。從舊位置移除標籤並將其設置在新的Positon上。
見How to change the order of the TabItem in the wpf TabControl
如果您正在使用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如此無法測試,你可以根據你的想法重新獲得它。
使用以下解決方案:
TabItem tempTab = new TabItem();
tempTab = control.Items[0] as TabItem;
control.Items[0] = control.Items[1];
control.Items[1] = tempTab;
這肯定會工作,你必須從代碼做後面。
在該示例中,爲什麼再次插入0索引而不考慮刪除索引? – nag 2012-01-12 04:51:57
它給了我錯誤'元素已經有了一個邏輯父項。它必須與舊的父母分離後才能與新的父母連接。「 – nag 2012-01-12 05:03:52