c# - 如何在选项卡控件对象中禁用选项卡换行

标签 c# wpf tabcontrol word-wrap

我正在 WPF/C# 中设计 GUI,并使用 Tab Control 对象。我的选项卡控件将包含很多选项卡,我想知道如何禁用对象的默认“包装”行为。基本上与 this 相反问题。我该怎么做呢?

谢谢。

最佳答案

只需更改 ItemsPanel:

<TabControl ....>
   <TabControl.ItemsPanel>
     <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
     </ItemsPanelTemplate>
   </TabControl.ItemsPanel>
  ....
</TabControl>

编辑:

刚测试过,不行。 @SimonBelanger 是对的。

这是 Default Template 的副本TabControl 的一部分,稍作修改,将 TabPanel 更改为水平 StackPanel:

<TabControl>
        <TabControl.Template>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="ColumnDefinition0"/>
                        <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                        <RowDefinition x:Name="RowDefinition1" Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1" Orientation="Horizontal"/>
                    <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                        <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="TabStripPlacement" Value="Bottom">
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                        <Setter Property="Margin" TargetName="HeaderPanel" Value="2,0,2,2"/>
                    </Trigger>
                    <Trigger Property="TabStripPlacement" Value="Left">
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                        <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                        <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                        <Setter Property="Margin" TargetName="HeaderPanel" Value="2,2,0,2"/>
                    </Trigger>
                    <Trigger Property="TabStripPlacement" Value="Right">
                        <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                        <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                        <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                        <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                        <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                        <Setter Property="Margin" TargetName="HeaderPanel" Value="0,2,2,2"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </TabControl.Template>

      ...            
    </TabControl>

关于c# - 如何在选项卡控件对象中禁用选项卡换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17794252/

相关文章:

c# - 在保留元数据的同时调整大小和保存图像

c# - 在 WinForms 选项卡控件中,我如何知道您正在从/向哪个选项卡移动?

c# - 使用 Linq 进行 ObjectDataSource : How to transform datetime using ToShortTimeString?

c# - 如何以最少的代码重复将不同的 GroupBy 存储在一个变量中?

c# - 使用属性路由时查询字符串不起作用

wpf - TreeView 项 : How to make selection change occur on Click instead of MouseDown?

c# - 游戏开发 - 如何将 'command line arguments' 传递给 Unity 独立构建?

.net - 如何使WPF窗口在任务栏上闪烁?

c# - 绑定(bind)在 Mvvm wpf 和 controltab 中不起作用

c# - 如何从 wpf 中的 Web 浏览器控件获取文档的标题