设置Popup的父级 [英] Setting the parent of a Popup

查看:97
本文介绍了设置Popup的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置Popup的Parent属性?使用以下代码,Popup的Parent属性为null。



How is the Parent property of a Popup set? With the following code, the Parent property of the Popup is null.

var text = new TextBox();
text.Text = "Popup Text";

var popup = new Popup();
popup.Child = text;
popup.PlacementTarget = button;
popup.Placement = PlacementMode.Bottom;
popup.IsOpen = true;





其中按钮是一个以XAML命名的按钮。



如果我将一个ComboBox添加到我的XAML中,它也可以将其内容可视化为Popup,但是它将其Parent属性设置为Grid。它是如何设法做到的?



where button is a Button named in XAML.

If I add a ComboBox to my XAML, it is visualizing its content as a Popup as well, but it has its Parent property set to a Grid. How does it manage to do that?

推荐答案

我的问题是基于我的Popup没有父级的事实。如果我使用Snoop调查ComboBox,他也使用Popup来表示下拉列表中的项目,看起来Microsoft自己有能力设置父项,因为ComboBox的Popup有一个Grid作为父项,但是当我创建时弹出窗口并设置PlacementTarget,Parent仍然为null。由于Parent属性是只读的,我不知道如何设置父项,或者如何在内部设置Microsoft。
My issue is based on the fact that my Popup doesn't have a parent. If I investigate the ComboBox using Snoop, who also uses a Popup to represent the items in the drop down, it seems that Microsoft themself have the ability to set a parent since the Popup of a ComboBox have a Grid as parent, but when I create a Popup and set PlacementTarget, Parent is still null. And since the Parent property is read-only, I have no idea how to set the parent, or what to do for Microsoft to set in internally.


您需要使用行为或扩展Popup(工具提示)。



You need use behavior or extend Popup (ToolTip).

public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var method = typeof(FrameworkElement).GetMethod("AddLogicalChild", BindingFlags.Instance | BindingFlags.NonPublic);
            method.Invoke(_coreParent, new object[] { Parent });
        }





创建课程时,需要设置coreParent。有用。在它之后,您可以使用RelativeSource,如果您的弹出窗口或工具提示是您控件的子项



When you create your class you need set coreParent. It works. After it you can use RelativeSource like if your popup or tooltip is child of your control


这篇关于设置Popup的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆