Thursday, April 10, 2008

WPF tips: Get your own logical parent

Have you ever wondered how does the Popup knows its inheritance context, its logical parent and the route for the routed events after you set PlacementTarget property? Or how does the ContextMenu knows where to fire the commands?
Today I have incidentally found the answer.
Whenever you have the FrameworkElement, that does not have any kind of parent set directly( neither visual, nor logical ), you can choose your parent yourself :)
To do so, you should override GetUIParentCore() method and return whatever logical parent you want – one-way link will be created, so you will know who is your parent, and your parent could unaware of your existence :)
BTW Popup returns its PlacementTarget property value in this method.

This mechanism could be used to implement custom inheritance context in some cases, so it is really important to remember of its existence.

1 comment:

Michael said...

Thanks. This was helpful for me