Find ancestor in DataTemplate

1k Views Asked by At

I'm trying to show popup on DataTemplate element TextEdit (from DevExpress), according to this and this topics I created something like that:

<DataTemplate x:Key="SomeTemplate">
    <dxe:TextEdit x:Name="SomeTextEdit" Text="{Binding DisplayText, Mode=OneWay}" 
                    EditMode="InplaceInactive">
        <dxe:TextEdit.ContextMenu>
            <ContextMenu/>
        </dxe:TextEdit.ContextMenu>
        <Popup PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dxe:TextEdit}}}" IsOpen="{Binding IsKeyboardFocused, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dxe:TextEdit}}, Mode=OneWay}">
            <TextBlock Background="White">
                <TextBlock.Text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</TextBlock.Text>
            </TextBlock>
        </Popup>
    </dxe:TextEdit>
</DataTemplate>

And it don't work, in Diagnostic Tool Events window I get message:

Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='DevExpress.Xpf.Editors.TextEdit', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'Popup' (Name=''); target property is 'PlacementTarget' (type 'UIElement')

Why is that?

1

There are 1 best solutions below

0
On BEST ANSWER

Popup will use different VisualTree and we cannot use RelativeSource binding method to find element from main VisualTree.Still you can set PlacementTarget via ElementName binding. You can use like this,

<Popup IsOpen="{Binding PlacementTarget.IsKeyboardFocused, RelativeSource={RelativeSource Mode=Self},Mode=OneWay}" PlacementTarget="{Binding ElementName=SomeTextEdit}">
    <TextBlock Background="White" Text="Hi" />
</Popup>