wpf - 如何在 TabControl 模板中获取所选项目的标题?

标签 wpf xaml header tabcontrol tabitem

我正在尝试获取 headerselected item在我的模板中 TabControl但我做不到。我尝试了几种解决方案,但都没有用:

没有结果:

<ContentPresenter ContentSource="{TemplateBinding SelectedItem}"/>

错误的编译(因为 SelectedItem 的类型是对象,而不是 HeaderedContentControl ):

<ContentPresenter ContentSource="{TemplateBinding SelectedItem.Header}"/>

在 C# 中很容易获得它,但我想将它放在我的 TabControl 模板中。

有人知道吗?

谢谢

最佳答案

这应该可以解决问题:

<TextBlock Text="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" />

这是一个完整的 xaml 演示:

<Window x:Class="WpfApplication21.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication21"
            Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="TabControlControlTemplate" TargetType="{x:Type TabControl}">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Selected Item header: " />
                    <TextBlock Text="{Binding SelectedItem.Header, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" />
                </StackPanel>

                <Grid ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="ColumnDefinition0"/>
                        <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                        <RowDefinition x:Name="RowDefinition1" Height="*"/>
                    </Grid.RowDefinitions>
                    <TabPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="True" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                    <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                        <ContentPresenter x:Name="PART_SelectedContentHost" ContentTemplate="{TemplateBinding SelectedContentTemplate}" Content="{TemplateBinding SelectedContent}" ContentStringFormat="{TemplateBinding SelectedContentStringFormat}" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </Grid>
            </StackPanel>
            <ControlTemplate.Triggers>
                <Trigger Property="TabStripPlacement" Value="Bottom">
                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                    <Setter Property="Margin" TargetName="HeaderPanel" Value="2,0,2,2"/>
                </Trigger>
                <Trigger Property="TabStripPlacement" Value="Left">
                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                    <Setter Property="Margin" TargetName="HeaderPanel" Value="2,2,0,2"/>
                </Trigger>
                <Trigger Property="TabStripPlacement" Value="Right">
                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                    <Setter Property="Margin" TargetName="HeaderPanel" Value="0,2,2,2"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </Window.Resources>

    <TabControl Template="{DynamicResource TabControlControlTemplate}">
        <TabItem Header="Tab 1" />
        <TabItem Header="Tab 2" />
        <TabItem Header="Tab 3" />
        <TabItem Header="Tab 4" />
    </TabControl>
</Window>

Header binding

关于wpf - 如何在 TabControl 模板中获取所选项目的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283412/

相关文章:

c# - 如何处理 ResourceDictionary 中定义的 ControlTemplate 中的 EventHandlers?

html - html 中的中心 Logo

wpf - WPF:UI线程被阻止时更新鼠标指针

c# - 未找到列表框 DisplayMemberPath 属性

c# - 是否可以为 path.Data 设置动画

xaml - WinRT和Light主题

c# - 获取没有 Content-Disposition 的文件名

c - C中的头文件递归

c# - 带有绑定(bind)的 MVVM 菜单栏

c# - 数据网格单元格行点击事件