如何使用 wpf 选项卡项目行中的剩余空间

发布于 2024-09-13 19:42:44 字数 509 浏览 11 评论 0原文

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

我想我可以使用具有不同样式的“假”TabItem,并将我的东西放在 TabItem.Header 中,但我希望有更好的方法。

解决方案

根据下面的答案,我通过将 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>

Upper part of TabControl consists of TabItem controls. Is there a way to reuse remaining space there to put some WPF content?

I think I could use a "fake" TabItem with different styling and put my stuff in TabItem.Header but I was hoping there's a better way.

Solution

Based on the answer below, I got the desired behavior by wrapping TabPanel in the template below within e.g. StackPanel and adding my additional content after it.

<StackPanel Orientation="Horizontal">
   <TabPanel 
    Grid.Row="0"
    Panel.ZIndex="1" 
    Margin="0,0,4,-1" 
    IsItemsHost="True"
    Background="Transparent" />
    <TextBlock>Foo</TextBlock>
</StackPanel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2024-09-20 19:42:44

我尝试了一种不同的方法,即创建另一个与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>

...您将得到:

在 tabcontrol 选项卡旁边的空白区域添加控件

I tried a different way, which was to create another grid that occupies the same space as the TabControl, ie both are in Grid.Row=0. I have bound the grid height to the height of the first tab so if the tabs change height the other controls will remain centered. I set MinWidth on the window so the controls dont overlap the tabs.

Paste this code into a new WPF Window...

<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>

...and you will get this:

Adding controls in empty space next to tabcontrol tabs

蹲墙角沉默 2024-09-20 19:42:44

您可以使用模板并使其执行您想要的任何操作,这就是 WPF 的强大功能。 这里是一篇关于自定义TabControl 和 TabItem 控件。

<编辑从“打开代码”文章中为 TabControl 模板添加代码>

<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>

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

You can use a template and make it do whatever you want, that is the power of WPF. Here is a nice article on customizing the TabControl and the TabItem controls.

< EDIT Adding code for TabControl template from Switch On The Code article>

<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>

all you have to do is add your content to the Template, the part that holds the tab items is the <TabControl>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文