gpt4 book ai didi

wPF VisualTreeHelper.GetParent 返回错误的类?

转载 作者:行者123 更新时间:2023-12-03 23:10:44 26 4
gpt4 key购买 nike

我定义了以下 XAML。

<Popup x:Class="EMS.Controls.Dictionary.MapTip"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
PopupAnimation="Slide"
AllowsTransparency="True" Placement="Mouse"
x:Name="root"
>

<Popup.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Popup.Resources>
<Viewbox x:Name="viewBox" IsHitTestVisible="True">
<Grid Background="Transparent" Name="mainGrid">

</Grid>
</Viewbox>
</Popup>

如果我使用 VisualTreeHelper.GetParent 从“mainGrid”走上可视化树,我最终会得到 System.Windows.Controls.Primitives.PopupRoot,但永远不会得到 Popup 本身。任何对为什么会这样以及我能做些什么有理论的人?我需要 Popup 而不是 PopupRoot。

TIA。

最佳答案

弹出窗口中的内容被添加到不同的可视化树中,其父对象是 PopupRoot,但您可以使用逻辑树帮助器通过以下代码段获取弹出窗口:

LogicalTreeHelper.GetParent()

来自 MSDN:

When you add content to a Popup control, the Popup control becomes the logical parent to the content. Similarly, the Popup content is considered to be the logical child of the Popup. The child content is not added to the visual tree that contains the Popup control. Instead, the child content is rendered in a separate window that has its own visual tree when the IsOpen property is set to true.



++

关于wPF VisualTreeHelper.GetParent 返回错误的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3794154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com