代码之家  ›  专栏  ›  技术社区  ›  Krzysztof Kowalczyk

如何使用wpf tab items行中的剩余空间

  •  0
  • Krzysztof Kowalczyk  · 技术社区  · 14 年前

    TabControl的上部由TabItem控件组成。有没有办法重用剩余的空间来放置一些WPF内容?

    我想我可以用一个不同样式的“假”标签,把我的东西放在标签上,但我希望有更好的方法。

    解决方案

    根据下面的答案,我通过在下面的模板中包装TabPanel(例如StackPanel)并在其后面添加我的附加内容,获得了所需的行为。

    <StackPanel Orientation="Horizontal">
       <TabPanel 
        Grid.Row="0"
        Panel.ZIndex="1" 
        Margin="0,0,4,-1" 
        IsItemsHost="True"
        Background="Transparent" />
        <TextBlock>Foo</TextBlock>
    </StackPanel>
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Muad'Dib    14 年前

    你可以使用一个模板,让它做任何你想做的,这就是WPF的力量。 Here

    <

    <Style  TargetType="{x:Type TabControl}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TabControl}">
            <Grid>
              <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
              </Grid.RowDefinitions>
              <TabPanel 
                 Grid.Row="0"
                 Panel.ZIndex="1" 
                 Margin="0,0,4,-1" 
                 IsItemsHost="True"
                 Background="Transparent" />
              <Border 
                 Grid.Row="1"
                 BorderBrush="Black" 
                 BorderThickness="1" 
                 CornerRadius="0, 12, 12, 12" >
                <Border.Background>
                  <LinearGradientBrush>
                    <GradientStop Color="LightBlue" Offset="0" />
                    <GradientStop Color="White" Offset="1" />
                  </LinearGradientBrush>
                </Border.Background>
                <ContentPresenter ContentSource="SelectedContent" />
              </Border>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    

    您所要做的就是将内容添加到模板中,保存选项卡项的部分就是 <TabControl>

        2
  •  2
  •   TripleAntigen    8 年前

    我尝试了另一种方法,即创建另一个与TabControl占用相同空间的网格,即两者都在grid.Row=0中。我已将网格高度绑定到第一个选项卡的高度,因此如果选项卡更改高度,其他控件将保持居中。我在窗口上设置了MinWidth,这样控件就不会与选项卡重叠。

    将此代码粘贴到新的WPF窗口。。。

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="306" Width="490" MinWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TabControl Grid.Row="0" x:Name="tabControl">
            <TabItem x:Name="tabItem" Header="TabItem" Height="50">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    
        <Grid Grid.Row="0" Height="{Binding ActualHeight, ElementName=tabItem}" 
               VerticalAlignment="Top" Margin="0,2,0,0">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" 
                        VerticalAlignment="Center" Margin="20,0">
                <TextBlock VerticalAlignment="Center" Margin="10,0" FontSize="16" 
                           Foreground="Red" FontFamily="Calibri">My Text</TextBlock>
                <Button Content="My Button" />
            </StackPanel>
        </Grid>
    
      </Grid>
    </Window>
    

    ……你会发现:

    Adding controls in empty space next to tabcontrol tabs