如何使用 WPF 网格填充选项卡项目区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19523026/
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
How to Fill Tab Item Area with WPF Grid
提问by user2859487
How does one get a grid to fill the lower portion of a tab item and make is size correctly? (personally I would call that area the page). As it stands right now I have a grid at my layout root and then a tab control with several tabs on it. I have tried to drag and drop a grid onto the tab control; however, it it's always set to the fixed size that I initially created it for.
如何获得一个网格来填充选项卡项目的下部并使其大小正确?(我个人将该区域称为页面)。就目前而言,我的布局根目录中有一个网格,然后是一个带有多个选项卡的选项卡控件。我试图将网格拖放到选项卡控件上;但是,它始终设置为我最初为其创建的固定大小。
<Grid x:Name="LayoutRoot" Margin="0,0,-1,0" Grid.IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="1497*"/>
<ColumnDefinition Width="116*"/>
<ColumnDefinition Width="36*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*"/>
<RowDefinition Height="24*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="27*"/>
<RowDefinition Height="597*"/>
<RowDefinition Height="0*"/>
</Grid.RowDefinitions>
<Rectangle Grid.ColumnSpan="4" Fill="#FF080808" Margin="-1,0,0,0" Stroke="#FF0061FF" Grid.RowSpan="3"/>
<Image Source="LP_Script Logo_black.jpg" Margin="32,18,1206,27" Grid.Column="1" Stretch="Fill" MinWidth="250" MinHeight="56" ScrollViewer.CanContentScroll="True"/>
<TextBox x:Name="ProgramSearchTextBox" TextWrapping="Wrap" Text="" KeyDown="ProgramSearchTextBox_KeyDown" PreviewKeyDown="ProgramSearchTextBox_PreviewKeyDown" Margin="2,1,2,0" HorizontalContentAlignment="Right" Grid.Column="2" Grid.Row="1" />
<TabControl x:Name="mainMenuTabControl" VerticalAlignment="Stretch" Height="Auto" Width="Auto" PreviewKeyDown="mainMenuTabControl_PreviewKeyDown" Grid.ColumnSpan="4" Margin="-1,0,1,0" Grid.Row="3" BorderThickness="1,1,1,0" Grid.RowSpan="3">
<TabItem x:Name="homeTab" Header="Home">
<TabItem.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFC1C0C0" Offset="1"/>
</LinearGradientBrush>
</TabItem.Background>
<DockPanel Height="Auto" Width="Auto" d:IsHidden="True">
<Grid x:Name="dashboard" Background="#FFE5E5E5" RenderTransformOrigin="0.5,0.5" Width="1636">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1" ScaleX="-1"/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
</Grid>
</DockPanel>
</TabItem>
<TabItem x:Name="fileMaintTab" Header="File Maintenance" KeyDown="fileMaintTab_KeyDown">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition/>
<ColumnDefinition Width="336*"/>
<ColumnDefinition Width="1316*"/>
</Grid.ColumnDefinitions>
<TreeView x:Name="FileMaintTreeView" Background="#FF747474" BorderThickness="1,1,5,1" BorderBrush="#FF0090FD" SelectedItemChanged="FileMaintTreeView_SelectedItemChanged" RenderTransformOrigin="0.5,0.5" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Margin="0,0,1,0">
<TreeView.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</TreeView.RenderTransform>
回答by Sheridan
Here is a small example to demonstrate this for you:
这是一个小例子来为你演示这一点:
<Grid>
<TabControl>
<TabItem Header="Whole Row Tab">
<Grid Background="Blue">
<TextBlock Text="I'm in the whole of this tab" />
</Grid>
</TabItem>
<TabItem Header="Half Row Tab">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="1" Background="Red">
<TextBlock Text="I'm in the bottom half of this tab" />
</Grid>
</Grid>
</TabItem>
<TabItem Header="Another Half Row Tab">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="1" Background="Red">
<TextBlock Text="I'm in the bottom half of this tab" />
</Grid>
</Grid>
</TabItem>
<TabItem Header="Varied Row Tab">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="Green">
<TextBlock Text="I'm in the top quarter of this tab" />
</Grid>
<Grid Grid.Row="3" Background="Green">
<TextBlock Text="I'm in the bottom quarter of this tab" />
</Grid>
</Grid>
</TabItem>
</TabControl>
</Grid>
回答by Mash
For the Gridunderneath your TabItem, try setting up your definitions, like this:
对于Grid下面的TabItem,请尝试设置您的定义,如下所示:
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
For your TreeViewdefinition, I changed it to the following:
对于您的TreeView定义,我将其更改为以下内容:
<TreeView x:Name="FileMaintTreeView" Background="#FF747474" BorderBrush="#FF0090FD" SelectedItemChanged="FileMaintTreeView_SelectedItemChanged" Grid.Column="0" Grid.Row="0">
(I removed things like margins and whatnot, you can put them back in if you want...
(我删除了诸如边距之类的东西,如果你愿意,你可以把它们放回去......
That will at least get your TreeViewto fill up the entire TabItem.
这至少会让你TreeView填满整个TabItem.
If you wanted the TabItemto have something else on the page... let's say some control next to the TreeView, then you can add another ColumnDefinition. Width="*" means it will fill up the entire remaining space. Width="Auto" means it will only fill the size of the controls nested within that column/row.
如果您希望在TabItem页面上有其他内容...假设在 旁边有一些控件TreeView,那么您可以添加另一个ColumnDefinition. Width="*" 表示它将填满整个剩余空间。Width="Auto" 意味着它只会填充嵌套在该列/行中的控件的大小。
回答by Dongdong
For me, finally I found reason from TabControlStyle.xaml(which is a style file), by adding following settings:
对我来说,最后我TabControlStyle.xaml通过添加以下设置找到了原因(这是一个样式文件):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
<Style.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Setter>
</Style>
</Style.Resources>
</Style>
</ResourceDictionary>

