如何使 WPF TabControl 在 Windows 窗体中使用 MultiLine = false 出现(默认)

发布于 2024-08-04 05:18:37 字数 1212 浏览 6 评论 0 原文

在 Windows 窗体中,TabControl 的默认行为是,如果选项卡占用太多空间 (MultiLine = false),则将选项卡溢出到可滚动区域。

在 WPF 中实现此行为的最佳方法是什么?

更新

我试图使用 TabControl.ItemsPanel 找到解决方案,但似乎我放入其中的任何内容都被完全忽略,因此出于这个原因,我走了一条艰难的道路,从 TabControl.Template 开始,令人难以置信的是,如果事实证明这是正确的方法,我们就必须这样做。

远未完成,我对问题的起始解决方案如下。

<TabControl>
  <TabControl.Template>
    <ControlTemplate TargetType="{x:Type TabControl}">
      <DockPanel>
        <ScrollViewer DockPanel.Dock="Top"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Disabled">
          <StackPanel Orientation="Horizontal" IsItemsHost="True" />
        </ScrollViewer>
        <ContentPresenter ContentSource="SelectedContent" />
      </DockPanel>
    </ControlTemplate>
  </TabControl.Template>
  <TabItem Header="One">First</TabItem>
  <TabItem Header="Two">Second</TabItem>
  <TabItem Header="Three">Third</TabItem>
  <TabItem Header="Four">Fourth</TabItem>
  <TabItem Header="Five">Fifth</TabItem>
</TabControl>

In Windows Forms the default behaviour of a TabControl is to have the tabs spill out to a scrollable area if they consume too much space (MultiLine = false).

What is the best approach to achieving this behavior in WPF?

UPDATE

I was trying to find a solution using TabControl.ItemsPanel but it seems anything I put in there gets completely ignored, so for this reason I've gone the hard way and started with TabControl.Template which is mind boggling that we have to do it this way if it turns out to be the correct approach.

Extremely far from being complete, my starting solution to the problem is as follows.

<TabControl>
  <TabControl.Template>
    <ControlTemplate TargetType="{x:Type TabControl}">
      <DockPanel>
        <ScrollViewer DockPanel.Dock="Top"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Disabled">
          <StackPanel Orientation="Horizontal" IsItemsHost="True" />
        </ScrollViewer>
        <ContentPresenter ContentSource="SelectedContent" />
      </DockPanel>
    </ControlTemplate>
  </TabControl.Template>
  <TabItem Header="One">First</TabItem>
  <TabItem Header="Two">Second</TabItem>
  <TabItem Header="Three">Third</TabItem>
  <TabItem Header="Four">Fourth</TabItem>
  <TabItem Header="Five">Fifth</TabItem>
</TabControl>

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

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

发布评论

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

评论(5

吹梦到西洲 2024-08-11 05:18:37

在制作 TabControl 时,选项卡沿左侧垂直堆叠,我为您找到了这个解决方案:

http://www.blogs.intuidev.com/post/2010/02/10/TabControlStyling_PartThree.aspx

相当令人印象深刻的东西!

In working to make a TabControl where the tabs are stacked vertically along the left, I found this solution for you:

http://www.blogs.intuidev.com/post/2010/02/10/TabControlStyling_PartThree.aspx

Pretty impressive stuff!

给妤﹃绝世温柔 2024-08-11 05:18:37

您替换模板的解决方案似乎是执行此操作的最佳方法。 TabItems 的默认面板是 TabPanel,我在其上没有看到任何类似“应该换行”属性的内容。

该文档包含使用不同 TabPanel 替换 TabControl 模板的示例:

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tabpanel.aspx

Your solution to replace the template seems to be the best way to do this. The default panel for the TabItems is a TabPanel, and I don't see anything like a "should wrap" property on it.

The documentation contains an example of replacing the TabControl template with a different TabPanel:

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tabpanel.aspx

稀香 2024-08-11 05:18:37

几年前我遇到了同样的问题,我的解决方案是限制标题和包含它的面板的大小,当然你需要像你开始的那样制作自己的模板,而且我还需要实现一些滚动支持所以我在滚动查看器的左侧和右侧放置了两个重复按钮。

我的灵感来自于 wpf 中名为 IE tabs 的代码项目中的一个不错的项目。
它与 wpf 一样古老并且运行良好

i had the same problem few years ago, my solution was to limit the size of the header, and the panel that contains it, of course you need to make your own template like what you started, and also i need to implement some scrolling support so i put two repeat buttons at the left and right side of the scroll viewer.

my inspiration was a nice project from code project called IE tabs in wpf.
it's old as wpf and works good

囍笑 2024-08-11 05:18:37

我知道这是一篇较旧的文章,但如果其他人在互联网上搜索此内容,我想添加另一个想法。

如果您将选项卡面板的宽度设置为更大的值(假设这不是允许用户继续在其中添加其他选项卡的选项卡面板)。如果您让用户向选项卡面板添加新选项卡,则需要添加滚动条。

I know this is an older post, but I wanted to add another idea should others be searching this on the internet.

If you set the width of the tabpanel to something larger it will be (assuming this is not a tabpanel that allows the user to continue to add other tabs in it). If you have the user adding new tabs to the tab panel, then a scroll bar will need to be added.

我喜欢麦丽素 2024-08-11 05:18:37

最简单的选项是在 TabControl 上设置 ItemsPanelTemplate。我认为默认是 WrapPanel,因此是多行行为。
例如将其更改为 StackPanel,并可能添加 ScrollViewer。

像这样的东西(只是在没有VS的情况下编码)

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

希望能有所帮助......

the easiest option is to set the ItemsPanelTemplate on the TabControl. I think the default is WrapPanel, hence the Multiline behaviour.
Change it to StackPanel for example and maybe add a ScrollViewer.

Something like this (just coding this without VS)

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

hope that helps a bit...

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