将 TabControl.Items 绑定到 MenuItem
我使用 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 内部,这实际上不是预期的结果(复选框位于标题区域中,并且有一个单独的边框围绕内部项目)。
有一个更好的方法吗?
提前致谢。
I'm using a TabControl as my main workspace in an application, and I'd like to add a "Window" menu item that lists the headers of open tabs. The active (i.e. - focused) tab should be checked.
I've tried using an ItemsTemplate as follows:
<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>
Each MenuItem is then "nested", so to speak, inside of another MenuItem, which really isn't the intended result (the checkbox is in the header area, and there is a separate border around the internal item).
Is there a better way to do this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然似乎应该有一种方法可以使用模板来做到这一点,但创建和使用样式似乎可行:
While it seems like there should be a way to do this with templates, creating and using a Style seems to work:
Malcolm,在绑定到 MenuItem 时,您需要使用 IsSelected 而不是 IsFocused。
如果您确实使用 IsSelected 而不是 IsFocused,您还可以将 IsSelected 与 Mode=TwoWay 绑定,这样您就不必使用 Click 处理程序来选择适当的 TabItem。
Malcolm, you'll want to use IsSelected instead of IsFocused when binding to the MenuItem.
If you do use IsSelected instead of IsFocused, you'll also be able to bind IsSelected with a Mode=TwoWay so that you don't have to use a Click handler to select the appropriate TabItem.