使用动态选项卡将 MenuItem 的 IsChecked 绑定到 TabItem 的 IsSelected [英] Binding MenuItem's IsChecked to TabItem's IsSelected with dynamic tabs

查看:34
本文介绍了使用动态选项卡将 MenuItem 的 IsChecked 绑定到 TabItem 的 IsSelected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选项卡项列表,其中动态添加了视图.每次用户添加视图时,都会创建一个新的选项卡项.我现在正在尝试将菜单绑定到 tabcontrol 的项目,以便用户可以从菜单中选择当前处于活动视图的视图.

I have a list of tab items that have views dynamically added to them. Every time a user adds a view, a new tab item is created. I'm now trying to bind a menu to a tabcontrol's items so that a user can select from a menu which view is currently the active view.

我的菜单是这样绑定的:

My menu is bound as such:

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

这很好用并且达到了预期的效果(每个菜单项都是所有打开的选项卡的列表).

This works fine and has the desired effect (each menu item is a listing of all the open tabs).

我有以下样式将菜单项绑定到选项卡项的 IsSelected 属性:

I have the following style that binds menu items to the IsSelected property of the tab items:

<Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}" />

我的问题是,此绑定不起作用.绑定错误消息指出它无法在视图对象上找到 IsSelected 属性.我不希望它使用特定视图,而是希望它查看视图当前绑定到的选项卡项.

My problem is, this binding doesn't work. The binding error message is stating that it can't find the IsSelected property on the view object. I don't want it to use the specfic view, rather, I want it to look at the tab item that the view is currently bound to.

我尝试了以下方法,但仍然出现绑定错误:

I've tried the following, but still get a binding error:

<Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=TabItem}}}" />

这表明它无法为每个菜单项找到 TabItem 类型的祖先(这是有道理的,因为菜单项的祖先不是它所绑定的.)

Which states that it can't find an ancestor of type TabItem for each menu item (which makes sense as the menu item's ancestors are not what it is bound to.)

有什么方法可以访问作为绑定传入的项的父项,以便我可以绑定到其属性?

Is there any way I can get access to the parent of the item that is coming in as a binding so I can bind to its properties?

更新:

根据 Yadyn 的建议,我决定创建一个值转换器并返回选项卡项.

Per Yadyn's advice, I decided to create a value converter and return tab items.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        ItemCollection ic = (ItemCollection)value;
        List<TabItem> tabItems = new List<TabItem>();
        foreach (var obj in ic) {
            tabItems.Add((TabItem)obj);
        }
        return tabItems;
    }

这使得将 IsSelected 绑定到 IsChecked 可以用于静态项目(已创建其选项卡项目的 TabControls),但对于动态添加的视图,永远不会调用 Convert 方法.这就像 TabControl 没有向其项目的活页夹发送更新,说明某些内容已更改.下面是 MenuItem 现在的连接方式:

This makes binding IsSelected to IsChecked work for static items (TabControls that have their tab items already created), but for the dynamically added views, the Convert method never gets called. It's like the TabControl is not sending out an update to binders of its items that something has changed. Here is how the MenuItem is wired up now:

<MenuItem Style="{StaticResource TabMenuButtonStyle}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=Items, Mode=OneWay, NotifyOnSourceUpdated=True,  Converter={StaticResource TabControlItemConverter}}" ItemContainerStyle="{StaticResource TabMenuItem}"></MenuItem>

推荐答案

TabControl.Items 将使您返回视图,因为这是您绑定到 TabControl 以获得动态选项卡视图的内容.

TabControl.Items will get you back the views, since that is what you've bound to your TabControl to have dynamic tab views.

不幸的是,没有可以直接绑定到 TabControl 的属性来获取 TabItem 的集合.这些实际上是 Items 绑定集合中每个项目的 ItemContainers.

Unfortunately, there isn't a property you can bind to on the TabControl directly that will get you a collection of the TabItems. These are actually the ItemContainers for each item in the Items bound collection.

你可以做的是创建一个转换器或其他东西.您可以尝试使用 myTabControl.ItemContainerGenerator.ContainerFromItem 并传入视图对象以取回包装它的实际 TabItem.然后您的 IsSelected 绑定将起作用.

What you might do is create a converter or something. You can try using myTabControl.ItemContainerGenerator.ContainerFromItem and pass in the view object to get back the actual TabItem that wraps it. Then your IsSelected binding will work.

您可能会考虑直接绑定到 TabControl 本身而不是 Items 属性.然后转换器可以轻松地对 ContainerFromItem 进行上述调用.然后,您必须通过自己枚举 Items 属性(为每个调用 ContainerFromItem)从转换器返回一个 List.

You might consider binding directly to the TabControl itself instead of the Items property. Then the converter can easily do the above call to ContainerFromItem. You'll then have to return a List<TabItems> from the converter by enumerating the Items property yourself (calling ContainerFromItem for each).

无论如何,希望这能让你走上正轨!

Anyway, hopefully this gets you on the right track!

这篇关于使用动态选项卡将 MenuItem 的 IsChecked 绑定到 TabItem 的 IsSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆