c# - 将 TabControl.Items 绑定(bind)到 MenuItem

标签 c# wpf datatemplate menuitem itemtemplate

我在应用程序中使用 TabControl 作为我的主要工作区,并且我想添加一个“窗口”菜单项来列出打开的选项卡的标题。应检查事件(即 - 聚焦)选项卡。

我试过如下使用 ItemsTemplate:

            <MenuItem Header="_Window" ItemsSource="{Binding ElementName=ux_workspace, Path=Items}">
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding Path=Header}" IsCheckable="True" IsChecked="{Binding IsFocused, Mode=OneWay}">
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>

每个 MenuItem 然后被“嵌套”,可以这么说,在另一个 MenuItem 内部,这实际上不是预期的结果(复选框位于标题区域,内部项目周围有一个单独的边框)。

有更好的方法吗?

提前致谢。

最佳答案

虽然看起来应该有一种方法可以使用模板来完成此操作,但创建和使用样式似乎可行:

<Style x:Key="TabMenuItem" TargetType="MenuItem">
    <Setter Property="Header" Value="{Binding Path=Header}" />
    <Setter Property="IsCheckable" Value="True" />
    <Setter Property="IsChecked" Value="{Binding Path=IsFocused, Mode=OneWay}" />
</Style>

<MenuItem Header="_Window"
    ItemsSource="{Binding ElementName=ux_workspace, Path=Items}"
    ItemContainerStyle="{StaticResource TabMenuItem}" />

关于c# - 将 TabControl.Items 绑定(bind)到 MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/997017/

相关文章:

xaml - 银光 4.0 : DataTemplate Error

c# - 自定义 TabItem 数据模板

c# - IdentityServer4 - 客户端的委托(delegate)访问 token

c# - 通用 Windows 项目 - HttpClient 异常

c# 使用 Linq 将数组中的数组投影到新数组上?

c# - 验证错误阻止调用属性 setter

c# - 当以编程方式更改属性时,PropertyChange 为 null

.net - 从 Winforms 迁移到 WPF

wpf - 在 WPF ListBox 中访问 ListBoxItem-Controls

C#类成员访问问题