{"id":443,"date":"2013-05-29T19:41:00","date_gmt":"2013-05-29T19:41:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=443"},"modified":"2025-02-25T13:26:18","modified_gmt":"2025-02-25T13:26:18","slug":"find-parent-control","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/find-parent-control","title":{"rendered":"Find the Parent Control of a Specific Type in WPF and Silverlight"},"content":{"rendered":"\n<p>The ability to find a parent control based on a type comes in real handy when dynamically composing views such as in a Prism application.\u00a0 There are tons of other scenarios where this is useful, but the most recent request for this was based off my <a href=\"http:\/\/brianlagunas.com\/xamdockmanageran-updated-prism-region-adapter\/\" target=\"_blank\" rel=\"noopener noreferrer\">XamDockManager Prism Region Adapter<\/a> post.\u00a0 Someone wanted to create a behavior that would be attached to their view, which is injected into a ContentPane, to control whether the ContentPane could be closed or not.\u00a0 Well since the cancellable Closing event exists on the ContentPane control itself and not on the View that was being injected, they needed access to the parent of type ContentPane from within the behavior.\u00a0 Well, lucky for them this is really easy to do.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static T FindParent&lt;T>(DependencyObject child) where T : DependencyObject\n{\n    \/\/get parent item\n    DependencyObject parentObject = VisualTreeHelper.GetParent(child);\n\n    \/\/we've reached the end of the tree\n    if (parentObject == null) return null;\n\n    \/\/check if the parent matches the type we're looking for\n    T parent = parentObject as T;\n    if (parent != null)\n        return parent;\n    else\n        return FindParent&lt;T>(parentObject);\n}<\/pre>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p>This simple code snippet will traverse up the visual tree of the control looking for a parent control matching the specific type provided.&nbsp; To use it, simple call the FindParent&lt;T&gt; method, where T is the type you are looking for, and then passing in the control in which you want to the parent of.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ContentPane _parentContentPane = FindParent&lt;ContentPane>(this);\nif (_parentContentPane != null)\n    _parentContentPane.Closing += _parentContentPane_Closing;<\/pre>\n\n\n\n<p>&nbsp;This particular example searches up the visual tree of a user control looking for the <a href=\"\/products\/wpf\/layouts\/dock-manager\" target=\"_blank\" rel=\"noopener noreferrer\">xamDockManager<\/a> ContentPane that contains the user control instance.&nbsp; Oh, and did I mention that this exact code snippet will work the same in both WPF and Silverlight?&nbsp; That was easy!<\/p>\n\n\n\n<p>Feel free contact me on my <a href=\"http:\/\/brianlagunas.com\/\" rel=\"noopener\">blog<\/a>, connect with me on Twitter (<a href=\"http:\/\/twitter.com\/brianlagunas\" rel=\"noopener\">@brianlagunas<\/a>), or leave a comment below for any questions or comments you may have.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know this is no ground breaking, mind blowing secret trick, but I have been getting a surprising number of questions regarding how to find the parent of a specific type for a control.<\/p>\n","protected":false},"author":43,"featured_media":2370,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wpf"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":3,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":1311,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/443\/revisions\/1311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2370"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}