1
我有一个WPF菜单和一个选项卡控件。我想要从我的选项卡控件上的TabItems集合中生成菜单项列表。我将我的选项卡控件绑定到一个集合以生成TabItems。我有一个TabItem样式,它使用ContentPresenter在TextBlock中显示TabItem文本。当我将选项卡项绑定到我的菜单时,菜单项是空白的。我正在使用样式设置器来设置菜单项名称,但我不知道我将用于设置MenuItem文本的TabItem属性。我的方案有解决方法吗?当我不知道提前选项卡的数量时,是否可以绑定到选项卡项目的Header属性?以下是我的xaml声明的副本。 Tab控件和物品:将WPF菜单项绑定到WPF选项卡控件项集合
<DataTemplate x:Key="ClosableTabItemTemplate">
<DockPanel HorizontalAlignment="Stretch">
<Button
Command="{Binding Path=CloseWorkSpaceCommand}"
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontSize="9"
FontWeight="Bold"
Margin="10,1,0,0"
Padding="0"
VerticalContentAlignment="Bottom"
Width="16" Height="16"
Background="Red"
/>
<ContentPresenter HorizontalAlignment="Center"
Content="{Binding Path=DisplayName}">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}"/>
</ContentPresenter.Resources>
</ContentPresenter>
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="10"
Background="#4C4C4C"/>
</DataTemplate>
我的菜单和局部样式上市
//我不知道哪个值是我该使用,因为我没有使用头
<Menu Background="Transparent">
<MenuItem Style="{StaticResource TabMenuButtonStyle}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type TabControl}}, Path=Items}"
ItemContainerStyle="{StaticResource TabMenuItem}">
</MenuItem>
</Menu>