Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How is the Parent property of a Popup set? With the following code, the Parent property of the Popup is null.

C#
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;


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?
Posted
Updated 22-Jun-16 23:12pm
Comments
Ravi Sant 23-Jan-12 9:44am    
not sure about this, have you tried:
UIElement parent = this.Parent;
parent.Child = popup;
something like this?

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.
 
Share this answer
 
You need use behavior or extend Popup (ToolTip).

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


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
 
Share this answer
 
Comments
Richard MacCutchan 23-Jun-16 5:27am    
This question was posted more than 4 years ago. Please do not resurrect dead questions like this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900