0

如何使用选项卡控件垂直“拉伸”来获得 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>

4

1 回答 1

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>

因此,如果我们用这样的 Grid 替换 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}" /> 

那么内容应该垂直拉伸。

于 2012-05-31T18:33:19.553 回答