WPF HeaderContent控件垂直对齐

时间:2012-05-31 18:05:01

标签: wpf tabs controls

如何使用标签控件将HCC一直垂直向下拉伸?我怎样才能做到这一点?

这是标头内容控件XAML:

<HeaderedContentControl 
 Content="{Binding Path=Workspaces}" 
 ContentTemplate="{StaticResource WorkspacesTemplate}" />  

以下是相应的样式信息:

<DataTemplate x:Key="WorkspacesTemplate">
    <TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}" 
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />
</DataTemplate>

<DataTemplate x:Key="ClosableTabItemTemplate">
 <DockPanel>
     <Button 
    Command="{Binding Path=CloseCommand}"
    Content="X"
    Cursor="Hand"
    DockPanel.Dock="Right"
    Focusable="False"
    FontFamily="Arial" 
    FontSize="9"
    FontWeight="Bold"  
    Margin="0,1,0,0"
    Padding="0"
    VerticalContentAlignment="Bottom"
    Width="16" Height="16" 
    />
        <ContentPresenter 
    Content="{Binding Path=DisplayName}" 
    />
 </DockPanel>

1 个答案:

答案 0 :(得分:2)

如果检查HeaderedContentControl的控件模板,您会发现它将内容放在StackPanel中,这就是内容不是垂直拉伸的原因。这是默认模板:

<Style x:Key="HeaderedContentControlStyle" 
       TargetType="{x:Type HeaderedContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <StackPanel>
                    <ContentPresenter ContentSource="Header"/>
                    <ContentPresenter/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

因此,如果我们用这样的网格替换StackPanel:

<Style x:Key="HeaderedContentControlStyle" 
       TargetType="{x:Type HeaderedContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ContentPresenter ContentSource="Header"/>
                    <ContentPresenter Grid.Row="1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并使用该风格

<HeaderedContentControl    
    Style="{StaticResource HeaderedContentControlStyle}"
    Content="{Binding Path=Workspaces}"    
    ContentTemplate="{StaticResource WorkspacesTemplate}" /> 

然后内容应该垂直延伸。