本文介绍了设置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?

推荐答案


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的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:45