问题描述
我一直在为这个问题寻找“纯”XAML 解决方案,但就是找不到。
我的目标是只在后面的代码中创建一个附加属性,但其余的应该只是 XAML,而不创建自定义控件或用户控件。但我不确定这是否可行,以及如何在 TabControl
模板中的嵌套元素和 TabItem
我的 string
类中有一个带有 [AttachedPropertybrowsableForType(typeof(TabItem))]
和/或 [AttachedPropertybrowsableForType(typeof(TabControl))]
的样板附加属性 MainWindow
和以下 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,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,8,8">
<Border BorderThickness="3,3,0" BorderBrush="DarkGray" CornerRadius="3"/>
<Border BorderThickness="2,1,1" BorderBrush="LightGray" CornerRadius="3"/>
<Border BorderThickness="1,1" BorderBrush="White" CornerRadius="3" Margin="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>
我希望在 TabControl
的 SelectedItemTitle
标签中显示相应的标题条目。
感谢任何提示,即使是明确的“那不可能”也是很高兴知道的,所以我可以停止尝试?
解决方法
附加属性的属性(子)路径需要用括号括起来:
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}}"