wpf TabControl 中等宽的选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27158128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tabs of equal width in TabControl
提问by davecove
I have 4 tabs at the top of a tab control. I would like for each tab to use 25% of the TabControl's width.
我在选项卡控件的顶部有 4 个选项卡。我希望每个选项卡使用 TabControl 宽度的 25%。
What is the correct way, using XAML, to do that?
使用 XAML 做到这一点的正确方法是什么?
Here is what I have tried:
这是我尝试过的:
<Grid HorizontalAlignment="Left" Height="458" Margin="10,65,0,0" VerticalAlignment="Top" Width="276">
<TabControl Grid.IsSharedSizeScope="True" HorizontalAlignment="Stretch">
<TabItem Header="Cameras">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="tabControl"/>
</Grid.ColumnDefinitions>
</Grid>
</TabItem>
<TabItem Header="MultiCam">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="tabControl"/>
</Grid.ColumnDefinitions>
</Grid>
</TabItem>
<TabItem Header="Search">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="tabControl"/>
</Grid.ColumnDefinitions>
</Grid>
</TabItem>
<TabItem Header="Admin" Margin="-2,-2,-10,-1">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="tabControl"/>
</Grid.ColumnDefinitions>
</Grid>
</TabItem>
</TabControl>
</Grid>
回答by Bizhan
Here's another trick, a Gridcan overlap any number of elements:
这是另一个技巧,aGrid可以重叠任意数量的元素:
<Grid>
<UniformGrid Columns="4" Margin="5,0">
<FrameworkElement x:Name="c1"/>
<!-- no need to add the other three -->
</UniformGrid>
<TabControl>
<TabItem Header="header" Width="{Binding ElementName=c1, Path=ActualWidth}"/>
<TabItem Header="header" Width="{Binding ElementName=c1, Path=ActualWidth}"/>
<TabItem Header="header" Width="{Binding ElementName=c1, Path=ActualWidth}"/>
<TabItem Header="header" Width="{Binding ElementName=c1, Path=ActualWidth}"/>
</TabControl>
</Grid>
a UniformGridthe same size of the TabControlis used to measure the width of each column. add only one FrameworkElementsince all TabItemsare the same size.
一个UniformGrid相同大小的TabControl用于测量每列的宽度。只添加一个,FrameworkElement因为所有TabItems的大小都相同。
回答by Shlomo
I have a funky solution. Not sure if it's optimal. Set the width of your TabItems to the following:
我有一个时髦的解决方案。不确定它是否是最佳的。将 TabItems 的宽度设置为以下内容:
<TabItem Header="Search" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type TabControl}}, Path=ActualWidth, Converter={StaticResource quarter}}">
</TabItem>
You'll need to add the converter as a static resource:
您需要将转换器添加为静态资源:
<Window.Resources>
<local:OneQuarterConverter x:Key="quarter" />
</Window.Resources>
And the code for the converter is as follows:
转换器的代码如下:
class OneQuarterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((double) value)/4.1;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((double)value) * 4.1;
}
}
Divided by 4.1 because of borders and margins, etc..
由于边框和边距等原因除以4.1。

