将 WPF 菜单项绑定到 WPF Tab Control Items 集合

发布于 2024-09-05 08:23:39 字数 1974 浏览 4 评论 0原文

我有一个 WPF 菜单和一个选项卡控件。我希望从选项卡控件上的 TabItems 集合生成菜单项列表。我将选项卡控件绑定到一个集合以生成 TabItems。我有一个 TabItem 样式,它使用 ContentPresenter 在 TextBlock 中显示 TabItem 文本。当我将选项卡项绑定到菜单时,菜单项为空白。我正在使用样式设置器来设置菜单项名称,但我不确定将使用 TabItem 的哪个属性来设置 MenuItem 文本。有适合我的场景的解决方法吗?当我事先不知道选项卡的数量时,是否可以绑定到选项卡项的 Header 属性?下面是我的 xaml 声明的副本。选项卡控件和项目:

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

I have a WPF Menu and a Tab control. I would like the list of menu items to be generated from the collection of TabItems on my tab control. I am binding my tab control to a collection to generate the TabItems. I have a TabItem style that uses a ContentPresenter to display the TabItem text in a TextBlock. When I bind the tab items to my menu the menu items are blank. I am using a Style Setter to set the Menu Item name, but I am not sure what property of the TabItem I would use to set the MenuItem text. Is there a workaround for my scenario? Is it possible to bind to the Header property of the tab item, when I do not know the number of tabs in advance? Below is a copy of my xaml declarations. Tab Control and items:

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

My Menu and partial style listing

//I am not sure which value am I suppose to use, since I am not using header

<Menu Background="Transparent">
    <MenuItem Style="{StaticResource TabMenuButtonStyle}"
                        ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type TabControl}}, Path=Items}"
                        ItemContainerStyle="{StaticResource TabMenuItem}">
    </MenuItem>
</Menu>

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

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

发布评论

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

评论(1

一紙繁鸢 2024-09-12 08:23:39

创建以下样式并绑定 Header 属性以在 ViewModel 中显示属性

<Style TargetType="{x:Type TabItem}">
        <Setter Property="Header" Value="{Binding PropertyInViewModel}" />
 </Style>

Create the following style and bind Header property to display property in ViewModel

<Style TargetType="{x:Type TabItem}">
        <Setter Property="Header" Value="{Binding PropertyInViewModel}" />
 </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文