如何在运行时更改 TabControl 的 TabItem 索引?

发布于 2024-12-26 04:40:57 字数 138 浏览 4 评论 0原文

我的 TabControl 中有五个 TabItem,我需要在运行时连续移动每个选项卡的位置。谁能告诉我如何在运行时将选项卡索引从一个位置更改为另一个位置。

谢谢,
@nagaraju。

I have five TabItem's in my TabControl and I need to move the position of each tab continuously at runtime. Can anyone tell me how can I change tab index from one position to another position at runtime.

Thanks,
@nagaraju.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽袖吟 2025-01-02 04:40:57

使用以下解决方案:

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

这肯定会起作用,并且您必须从代码后面执行此操作。

Use the below solution:

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

This will definitely work and you have to do from code behind.

冰之心 2025-01-02 04:40:57

如果您使用 ObservableCollection ,您只需更改集合中项目的位置,它将反映在视图中......

例如......

<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 中的 code>TabList ,该位置将相应更改...

在您的 ViewModel 中,

如果获取数据和设置命令,我有实现...这取决于您如何执行

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

上面的代码我给出 <代码>索引超出范围但是我离 IDE 还很远,所以无法测试你可以根据你的情况重新启动它。

If you are using ObservableCollection the you Just have to change the position of the Item in your collection it will be refelected in View...

For Example..

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

Here you just Change the Position of the item in TabList in ViewModel and that Position will be changed accordingly...

In Your ViewModel

I have the implementation if getting Data and Setting Up Commands... that up to you how you Do it

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

The above code my give INdex out of bound but I am no where near an IDE so cant test that you could reapir it according to you.

满天都是小星星 2025-01-02 04:40:57

您需要更改 TabControl.Items 集合。从旧位置移除标签并将其设置在新位置。

请参阅如何更改wpf TabControl 中的 TabItem

You need to change the TabControl.Items Collection. Remove the tab from the old Position and set it on a new Positon.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文