【问题标题】:Binding WPF menu items to WPF Tab Control Items collection将 WPF 菜单项绑定到 WPF 选项卡控件项集合
【发布时间】:2010-06-12 08:16:34
【问题描述】:

我有一个 WPF 菜单和一个 Tab 控件。我希望从我的选项卡控件上的 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>

【问题讨论】:

    标签: data-binding wpf-controls datatemplate


    【解决方案1】:

    创建如下样式并将Header属性绑定到ViewModel中的显示属性

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多