标签控件宽度wpf

发布于 2024-11-03 06:33:32 字数 116 浏览 2 评论 0原文

我有一个带有 n 个选项卡的 TabControl。 我试图限制 TabControl 的宽度,以便如果我只剩下一个选项卡,那么当我调整包含 Tabcontrol 的主窗口的大小时,它的标题将始终可见。 有什么建议吗?

I have a TabControl with n tabs.
I am trying to limit the TabControl's width so that if I have only one Tab left, then its header would be always visible when I resize the main window that contains the Tabcontrol.
Any suggestions?

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

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

发布评论

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

评论(1

来世叙缘 2024-11-10 06:33:32

我通常会在只剩下一项时隐藏该选项卡:

<TabControl ItemsSource="{Binding Data}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=Items.Count}"
                             Value="1">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

如果您想显示某些内容,您可以将其堆叠在上面并仅在计数降至 1 时才显示它。

编辑: 我可能应该提到你的问题实际上没有太大意义,你应该尝试更清楚地表达自己。关于您可能的意思的一种猜测是您希望最后一个剩余的选项卡拉伸整个可用宽度。这并不是那么简单,因为标题位于 TabPanel 中,不能简单地拉伸它。

一种近似方法是绑定到 TabControl 的宽度:

<TabControl.ItemContainerStyle>
    <Style TargetType="{x:Type TabItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=Items.Count}"
                     Value="1">
                <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=ActualWidth}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TabControl.ItemContainerStyle>

但这有点太大,您可能需要使用 ValueConverter

编辑2:像这样:

<Style TargetType="{x:Type TabItem}">
    <Style.Resources>
        <local:AddConverter x:Key="AddConverter"/>
    </Style.Resources>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=Items.Count}"
                 Value="1">
            <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource AncestorType=TabControl},
                                                     Path=ActualWidth,
                                                     Converter={StaticResource AddConverter},
                                                     ConverterParameter=-5}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
public class AddConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double input = (double)value;
        double input2 = double.Parse(parameter as string);
        return input + input2;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double input = (double)value;
        double input2 = double.Parse(parameter as string);
        return input - input2;
    }
}

I would normally hide the tab when only one item is left:

<TabControl ItemsSource="{Binding Data}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=Items.Count}"
                             Value="1">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

If you want to display something instead you could stack it above and show it only when the count drops to 1.

Edit: I probably should have mentioned that your question does in fact not make all too much sense, you should try to express yourself more clearly. One guess as to what you might have meant is that you want the last remaining tab to stretch the whole available width. This is not that simple because the header is in a TabPanel, it cannot simply be stretched.

One approximation would be to bind to the TabControl's width:

<TabControl.ItemContainerStyle>
    <Style TargetType="{x:Type TabItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=Items.Count}"
                     Value="1">
                <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=ActualWidth}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TabControl.ItemContainerStyle>

But this is a bit too large, you might want to subtract a small value by using a ValueConverter.

Edit2: Like this:

<Style TargetType="{x:Type TabItem}">
    <Style.Resources>
        <local:AddConverter x:Key="AddConverter"/>
    </Style.Resources>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=Items.Count}"
                 Value="1">
            <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource AncestorType=TabControl},
                                                     Path=ActualWidth,
                                                     Converter={StaticResource AddConverter},
                                                     ConverterParameter=-5}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
public class AddConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double input = (double)value;
        double input2 = double.Parse(parameter as string);
        return input + input2;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double input = (double)value;
        double input2 = double.Parse(parameter as string);
        return input - input2;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文