在DataTemplate中查找祖先

时间:2016-12-20 09:21:19

标签: wpf xaml popup devexpress datatemplate

我正在尝试在−D元素DataTemplate(来自DevExpress)上显示弹出窗口,根据thisthis主题,我创建了类似的内容:

TextEdit

它不起作用,在“诊断工具事件”窗口中,我收到消息:

<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>

为什么?

1 个答案:

答案 0 :(得分:2)

Popup将使用不同的VisualTree,我们无法使用RelativeSource绑定方法从主VisualTree中查找元素。您仍然可以通过PlacementTarget绑定设置ElementName 。你可以这样使用,

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