Bind to specific AncestorType, skipping derived types

时间:2018-06-19 11:09:38

标签: wpf data-binding relativesource

I'm using a DevExpress LayoutGroup, which constructs multiple children I have no influence on. The LayoutGroup has an IsCollapsible property, which, when true, adds a button to the group's header and the functionality of collapsing/expanding the group's content.

The constructed visual tree looks like this:

[LayoutControl]
  CollapsibleNavigationGroup [LayoutGroup]
    [GroupBox]
      [Container]
        BorderElement [Border]
          [LayoutControl]
            TitleElement [Container]
              TitleContent [LayoutControl]

This usually only works when clicking the button itself but I extended the functionality to clicking the header. Now, if the GroupBox is collapsible I want the cursor to change to a hand when hovering above the header, so I did this in its template:

<dxlc:LayoutControl x:Name="TitleContent"
                    Cursor="{Binding IsCollapsible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type dxlc:LayoutGroup}}, Converter={StaticResource BoolToCursorConverter}}">

However, LayoutControl is derived from LayoutGroup and thus FindAncestor does not actually find my CollapsibleNavigationGroup but the element's grand-parent, the nameless LayoutControl. Now I wonder, is there any way to tell the relative source binding to actually look for this exact type and ignore any derived types?

1 个答案:

答案 0 :(得分:1)

  

现在我想知道,有什么方法可以告诉相对源绑定实际寻找这个确切类型并忽略任何派生类型吗?

不,没有。但是您可以将AncestorLevel属性设置为2,以跳过AncestorType属性指定的类型的第一个祖先,例如:

<dxlc:LayoutControl ... Cursor="{Binding IsCollapsible, RelativeSource={RelativeSource Mode=FindAncestor, 
                        AncestorType={x:Type dxlc:LayoutGroup}, 
                        AncestorLevel=2}, Converter={StaticResource BoolToCursorConverter}}">