wPF VisualTreeHelper.GetParent返回错误的类?

Kla*_*Nji 13 wpf

我定义了以下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>
Run Code Online (Sandbox Code Playgroud)

如果我使用来自"mainGrid"的VisualTreeHelper.GetParent走向可视树,我最终得到System.Windows.Controls.Primitives.PopupRoot,但永远不会获得Popup本身.任何人都有关于为什么会这样做以及我能做些什么的理论?我需要Popup而不是PopupRoot.

TIA.

Jon*_*INE 17

弹出窗口中的内容将添加到不同的可视树中,父窗口是PopupRoot,但您可以使用逻辑树帮助程序获取此片段的弹出窗口:

LogicalTreeHelper.GetParent()
Run Code Online (Sandbox Code Playgroud)

来自MSDN:

向Popup控件添加内容时,Popup控件将成为内容的逻辑父级.同样,Popup内容被认为是Popup的逻辑子代.子内容未添加到包含Popup控件的可视树中.相反,当IsOpen属性设置为true时,子内容将在单独的窗口中呈现,该窗口具有自己的可视树.

++


rud*_*ler 5

尝试走逻辑树而不是视觉树

LogicalTreeHelper.GetParent()
Run Code Online (Sandbox Code Playgroud)


小智 5

LogicalTreeHelper也无法访问弹出窗口,最好的方法是尝试使用名称“PopupRoot”来比较GetType().Name.