XAML:如何将 TabControl 嵌套元素绑定到 TabItem 的 Attached 属性

XAML: How to bind TabControl nested element to Attached Property of TabItem

我一直在为这个问题寻找“纯粹的”XAML 解决方案,但就是找不到。 我的目标是 在代码后面创建一个附加的 属性,但其余的应该是 XAML,而不创建自定义控件或用户控件。但我不确定这是否可能,如果,如何在 TabControl 模板内的嵌套元素与 TabItem[= 中的附加 属性 集之间建立连接21=]

我会在 MainWindow [=32= 中附加 string 的 属性 样板,其中 [AttachedPropertyBrowsableForType(typeof(TabItem))] and/or [AttachedPropertyBrowsableForType(typeof(TabControl))] ] 和以下 XAML

<Window x:Class="AP_Test.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"
        xmlns:local="clr-namespace:AP_Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="800">

  <Window.Resources>
    <Style TargetType="{x:Type TabControl}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TabControl}">
            <Grid Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Name="ColumnDefinition0"/>
                <ColumnDefinition Name="ColumnDefinition1" Width="0"/>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition Name="RowDefinition0" Height="Auto"/>
                <RowDefinition Name="RowDefinition1" Height="*"/>
              </Grid.RowDefinitions>
              <TabPanel Name="headerPanel" 
                                  Background="Transparent" 
                                  Grid.Column="0" 
                                  IsItemsHost="true" 
                                  Margin="2,2,2,0" 
                                  Grid.Row="0" 
                                  KeyboardNavigation.TabIndex="1" 
                                  Panel.ZIndex="1"/>
              <Border 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">
                <DockPanel Background="White">
                  <Grid Name="TabControlHeader" DockPanel.Dock="Top" Height="65">
                    <Label x:Name="SelectedItemTitle" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Content="How to bind to AP ItemTitle?"/>
                  </Grid>

                  <Grid Name="Detail" Margin="8,0,8,8">
                    <Border BorderThickness="3,3,0,0" BorderBrush="DarkGray"  CornerRadius="3"/>
                    <Border BorderThickness="2,2,1,1" BorderBrush="LightGray" CornerRadius="3"/>
                    <Border BorderThickness="1,1,1,1" BorderBrush="White" CornerRadius="3" Margin="3,3,-1,-1" Padding="5">
                      <Viewbox>
                        <ContentPresenter Name="PART_SelectedContentHost" 
                                                              ContentSource="SelectedContent" 
                                                              Margin="{TemplateBinding Padding}" 
                                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                      </Viewbox>
                    </Border>
                  </Grid>
                </DockPanel>
              </Border>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  
  <TabControl x:Name="TabCtl">
    <TabItem Header="Tab1" local:MainWindow.ItemTitle="Tab1 Title" />
    <TabItem Header="Tab2" local:MainWindow.ItemTitle="Tab2 Title" />
    <TabItem Header="Tab3" local:MainWindow.ItemTitle="Tab3 Title" />
  </TabControl>
</Window>

我希望相应的标题条目显示在 TabControlSelectedItemTitle 标签中。

感谢任何提示,即使是明确的“那不可能”也很高兴知道,所以我可以停止尝试

附加 属性 的 属性(子)路径需要用括号括起来:

Content="{Binding Path=SelectedItem.(local:MainWindow.ItemTitle),
                  RelativeSource={RelativeSource AncestorType=TabControl}}"

详情见PropertyPath for Objects in Data Binding


甚至不需要附加的属性。您也可以使用 TabItem 的 Tag 属性 之类的

<TabItem Header="Tab1" Tag="Tab1 Title"/>

Content="{Binding Path=SelectedItem.Tag,
                  RelativeSource={RelativeSource AncestorType=TabControl}}"