如何右对齐最后一个 TabControl 选项卡?

发布于 2024-12-29 10:32:58 字数 65 浏览 0 评论 0原文

有没有办法使 TabControl 上的最后一个选项卡右对齐? 想让最后一个与前几个分开。

谢谢 !

Is there a way to make last tab on TabControl right-aligned ?
Want to make the last one separate from the first few.

Thanks !

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

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

发布评论

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

评论(3

吖咩 2025-01-05 10:32:58

这篇文章可能很旧,但我在寻找同一问题的答案时偶然发现了它,所以我想我会分享我最终得到的快速而肮脏的解决方案。

我只是将两个 TabControl 放在网格中,然后将其中一个 TabPanel 右对齐(感谢 Meleak):

 <Grid>
    <TabControl x:Name="_tabsRight" GotFocus="OnTabFocused" >
        <TabControl.Resources>
            <Style TargetType="TabPanel">
                <Setter Property="HorizontalAlignment" Value="Right"/>
            </Style>
        </TabControl.Resources>

        <TabItem x:Name="JustAHiddenTabItemToDeselectTheRealOne" Visibility="Hidden" />

        <!-- Last tab -->
        <TabItem Header="Last one" >
            <!-- Last content... -->
        </TabItem>

    </TabControl>
    <TabControl x:Name="_tabsLeft" GotFocus="OnTabFocused" >

        <!-- First tab -->
        <TabItem Header="1st" >
            <!-- First content... -->
        </TabItem>

        <!-- Second tab -->
        <TabItem Header="2nd" >
            <!-- Second content... -->
        </TabItem>

    </TabControl>
</Grid>

然后,在 OnTabFocused 事件处理程序中,当用户单击 TabItem:

private int _zIncrementor = 0;
/// <summary>
/// Hack to make two TabControls act as one.
/// </summary>
private void OnTabFocused(object sender, RoutedEventArgs e)
{
    var tab = (TabControl)sender;
    var otherTab = (tab == _tabsLeft) ? _tabsRight : _tabsLeft;

    Grid.SetZIndex(tab, ++_zIncrementor);
    otherTab.SelectedItem = null;
}

This post may be old, but i stumbled across it while searching for an answer to the same question, so i thought I'd share the quick and dirty solution I ended up with.

I just put two TabControls on top of each other in a Grid, and right-aligned the TabPanel on one of them (thanks go out to Meleak):

 <Grid>
    <TabControl x:Name="_tabsRight" GotFocus="OnTabFocused" >
        <TabControl.Resources>
            <Style TargetType="TabPanel">
                <Setter Property="HorizontalAlignment" Value="Right"/>
            </Style>
        </TabControl.Resources>

        <TabItem x:Name="JustAHiddenTabItemToDeselectTheRealOne" Visibility="Hidden" />

        <!-- Last tab -->
        <TabItem Header="Last one" >
            <!-- Last content... -->
        </TabItem>

    </TabControl>
    <TabControl x:Name="_tabsLeft" GotFocus="OnTabFocused" >

        <!-- First tab -->
        <TabItem Header="1st" >
            <!-- First content... -->
        </TabItem>

        <!-- Second tab -->
        <TabItem Header="2nd" >
            <!-- Second content... -->
        </TabItem>

    </TabControl>
</Grid>

Then, in the OnTabFocused event handler, we need to bring the bottom-most TabControl to the front when the user clicks a TabItem:

private int _zIncrementor = 0;
/// <summary>
/// Hack to make two TabControls act as one.
/// </summary>
private void OnTabFocused(object sender, RoutedEventArgs e)
{
    var tab = (TabControl)sender;
    var otherTab = (tab == _tabsLeft) ? _tabsRight : _tabsLeft;

    Grid.SetZIndex(tab, ++_zIncrementor);
    otherTab.SelectedItem = null;
}
无人问我粥可暖 2025-01-05 10:32:58

这里是一个关于模板化 TabControl 选项卡的示例项目。我可能会使用具有三列宽度“自动”、* 和“自动”的网格,并在第一列中放置一个 StackPanel 来保存第一组选项卡,然后将最后一个选项卡单独放置在最后一列中,中间为中间的列列是空的,只占用剩余空间。

Here is an example project on templating the TabControl tabs. I would probably use a Grid with three columns of width "Auto", * and "Auto" and put a StackPanel in the first column to hold the first set of tabs and then just the last tab by itself in the last column with the middle column being empty and just taking up the remaining space.

明媚殇 2025-01-05 10:32:58

如果您希望左侧有两个选项卡,右侧有一个选项卡,则可以在中间放置第三个不可见选项卡,并且不可见选项卡的宽度可以通过减去以下的宽度来计算WindowActualwidth 中的所有三个可见选项卡为我们提供了剩余空间。

这是示例代码

<TabControl x:Name="_tabsLeft" GotFocus="OnTabFocused" >

    <!-- First tab -->
    <TabItem Header="1st" >
        <!-- First content... -->
    </TabItem>

    <!-- Second tab -->
    <TabItem Header="2nd" >
        <!-- Second content... -->
    </TabItem>

     <!-- Third invisible tab -->
    <TabItem Header="Im not visible in UI" Visibility="Hidden" x:Name="invisibletab" >
        <!-- I'm not visible in UI... -->
    </TabItem>

    <!-- Last tab -->
    <TabItem Header="Last one" >
        <!-- Last content... -->
    </TabItem>

</TabControl>

后端代码:

public MainWindow()
{
    InitializeComponent();       
    this.SizeChanged += window_SizeChanged;
}
private void window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    invisibletab.Width = this.ActualWidth - 550; // where the 550 is the sum of the actual width of visible tabs
}

If you want to have two tabs at left and one at right, You can have the third invisible tab inbetween and width of the invisible tab can be calculated by subtracting the Width of all three visible tabs from the Actualwidth of the Window which gives us the remaining space.

Here is the sample code

<TabControl x:Name="_tabsLeft" GotFocus="OnTabFocused" >

    <!-- First tab -->
    <TabItem Header="1st" >
        <!-- First content... -->
    </TabItem>

    <!-- Second tab -->
    <TabItem Header="2nd" >
        <!-- Second content... -->
    </TabItem>

     <!-- Third invisible tab -->
    <TabItem Header="Im not visible in UI" Visibility="Hidden" x:Name="invisibletab" >
        <!-- I'm not visible in UI... -->
    </TabItem>

    <!-- Last tab -->
    <TabItem Header="Last one" >
        <!-- Last content... -->
    </TabItem>

</TabControl>

Backend Code:

public MainWindow()
{
    InitializeComponent();       
    this.SizeChanged += window_SizeChanged;
}
private void window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    invisibletab.Width = this.ActualWidth - 550; // where the 550 is the sum of the actual width of visible tabs
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文