我正在尝试在−D
元素DataTemplate
(来自DevExpress)上显示弹出窗口,根据this和this主题,我创建了类似的内容:
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>
为什么?
答案 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>