I'm using the TabControl
class in WPF and I've noticed that the content of each TabItem
has a default margin of 4 pixels on all sides.
我在WPF中使用了TabControl类,我注意到每个表项的内容在所有方面都有4个像素的默认边界。
Sample code:
示例代码:
<Window x:Class="TabControlPadding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TabControl Margin="10">
<TabItem Header="Tab 1">
<Grid Background="Pink"/>
</TabItem>
<TabItem Header="Tab 2">
<Grid Background="LightBlue"/>
</TabItem>
</TabControl>
</Grid>
</Window>
Screenshot:
截图:
I'd like to get rid of this margin (reduce it to zero), but I'd prefer not to have to completely replace templates or anything heavy like that.
我想去掉这个边距(减少到0),但是我不希望完全替换模板或其他类似的东西。
Is there a simple way I can do this in a very targeted manner?
有没有一种简单的方法可以让我有针对性地做这件事?
4 个解决方案
#1
41
Just set Padding to zero on the TabControl:
只需将TabControl上的填充设置为0:
<TabControl Margin="10" Padding="0">
The default style for TabControl sets the Padding to 4 and binds the Margin on the content host to the Padding on the TabControl.
TabControl的默认样式将填充设置为4,并将内容主机上的边距绑定到TabControl上的边距。
#2
3
If you're looking to make the pink box expand all the way to the black border line with no white in between, there is an easy way that doesn't involved making your own control template.
如果您希望使粉色框一直扩展到黑色边框线,中间没有白色,那么有一种简单的方法,不需要创建自己的控件模板。
The default style for TabItem has a margin of 4 around the content presenter. A quick way to compensate for this is to make the margin of the control inside the TabItem -4.
TabItem的默认样式在content presenter的周围有4个边距。一种快速的补偿方法是将控件的边界设置为-4。
<TabItem>
<Grid Margin="-4">
</Grid>
<TabItem>
#3
1
Write your own controltemplate for TabItems, see TabItem ControlTemplate Example
为TabItems编写自己的controltemplate,请参见TabItem controltemplate示例。
#4
0
Set the margin for the TabItem instead of Tab
为选项卡项而不是选项卡设置空白
<TabItem Margin="0,0,0,0"/>
Set the margin for the TabItem to 0, this will override the default margin and work as per your requirements
将TabItem的页边距设置为0,这将覆盖默认页边距,并按照您的要求工作
#1
41
Just set Padding to zero on the TabControl:
只需将TabControl上的填充设置为0:
<TabControl Margin="10" Padding="0">
The default style for TabControl sets the Padding to 4 and binds the Margin on the content host to the Padding on the TabControl.
TabControl的默认样式将填充设置为4,并将内容主机上的边距绑定到TabControl上的边距。
#2
3
If you're looking to make the pink box expand all the way to the black border line with no white in between, there is an easy way that doesn't involved making your own control template.
如果您希望使粉色框一直扩展到黑色边框线,中间没有白色,那么有一种简单的方法,不需要创建自己的控件模板。
The default style for TabItem has a margin of 4 around the content presenter. A quick way to compensate for this is to make the margin of the control inside the TabItem -4.
TabItem的默认样式在content presenter的周围有4个边距。一种快速的补偿方法是将控件的边界设置为-4。
<TabItem>
<Grid Margin="-4">
</Grid>
<TabItem>
#3
1
Write your own controltemplate for TabItems, see TabItem ControlTemplate Example
为TabItems编写自己的controltemplate,请参见TabItem controltemplate示例。
#4
0
Set the margin for the TabItem instead of Tab
为选项卡项而不是选项卡设置空白
<TabItem Margin="0,0,0,0"/>
Set the margin for the TabItem to 0, this will override the default margin and work as per your requirements
将TabItem的页边距设置为0,这将覆盖默认页边距,并按照您的要求工作