如何使用 wpf 选项卡项目行中的剩余空间
TabControl 的上部由 TabItem 控件组成。有没有办法重用那里的剩余空间来放置一些 WPF 内容?
我想我可以使用具有不同样式的“假”TabItem,并将我的东西放在 TabItem.Header 中,但我希望有更好的方法。
解决方案
根据下面的答案,我通过将 TabPanel 包装在下面的模板中(例如 StackPanel)并在其后添加附加内容来获得所需的行为。
<StackPanel Orientation="Horizontal">
<TabPanel
Grid.Row="0"
Panel.ZIndex="1"
Margin="0,0,4,-1"
IsItemsHost="True"
Background="Transparent" />
<TextBlock>Foo</TextBlock>
</StackPanel>
Upper part of TabControl consists of TabItem controls. Is there a way to reuse remaining space there to put some WPF content?
I think I could use a "fake" TabItem with different styling and put my stuff in TabItem.Header but I was hoping there's a better way.
Solution
Based on the answer below, I got the desired behavior by wrapping TabPanel in the template below within e.g. StackPanel and adding my additional content after it.
<StackPanel Orientation="Horizontal">
<TabPanel
Grid.Row="0"
Panel.ZIndex="1"
Margin="0,0,4,-1"
IsItemsHost="True"
Background="Transparent" />
<TextBlock>Foo</TextBlock>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了一种不同的方法,即创建另一个与TabControl占据相同空间的网格,即两者都在Grid.Row=0中。我已将网格高度绑定到第一个选项卡的高度,因此如果选项卡更改高度,其他控件将保持居中。我在窗口上设置了 MinWidth,以便控件不会与选项卡重叠。
将此代码粘贴到新的 WPF 窗口中...
...您将得到:
I tried a different way, which was to create another grid that occupies the same space as the TabControl, ie both are in Grid.Row=0. I have bound the grid height to the height of the first tab so if the tabs change height the other controls will remain centered. I set MinWidth on the window so the controls dont overlap the tabs.
Paste this code into a new WPF Window...
...and you will get this:
您可以使用模板并使其执行您想要的任何操作,这就是 WPF 的强大功能。 这里是一篇关于自定义TabControl 和 TabItem 控件。
<编辑从“打开代码”文章中为 TabControl 模板添加代码>
您所要做的就是将内容添加到模板中,保存选项卡项的部分是
You can use a template and make it do whatever you want, that is the power of WPF. Here is a nice article on customizing the TabControl and the TabItem controls.
< EDIT Adding code for TabControl template from Switch On The Code article>
all you have to do is add your content to the Template, the part that holds the tab items is the
<TabControl>