wpfxamlbindingfindancestor

Expander.IsExpaned only if current template is a descendant of a ToolTip


I am using a DataTemplate (a StaticResource) in several places in my application as in an ItemsControl, ContentControl and in a ToolTip.

The template contains an Expander, and I want its IsExpanded property to be true if the DataTemplate is located within a ToolTip.

Any recommendations?


Solution

  • This checks for the parent tooltip, if there is no parent tooltip, it will be evaluated to nothing = false:

    <Expander Name="expJobContacts" 
        IsExpanded="{Binding IsVisible, 
            RelativeSource={RelativeSource Mode=FindAncestor, 
                AncestorType={x:Type ToolTip}}, 
            Mode=OneTime}" />
    

    I tested and it works.