{"id":435,"date":"2013-01-29T22:45:13","date_gmt":"2013-01-29T22:45:13","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=435"},"modified":"2025-02-25T13:29:16","modified_gmt":"2025-02-25T13:29:16","slug":"dependencyproperty-by-name","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/dependencyproperty-by-name","title":{"rendered":"Get DependencyProperty by Name for WPF and Silverlight"},"content":{"rendered":"\n<p>Have you ever had the need to get the DependencyProperty of an element at runtime for any reason at all?\u00a0 If so, then this code snippet may be of use to you.<\/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 DependencyProperty GetDependencyPropertyByName(DependencyObject dependencyObject, string dpName)\n{\n    return GetDependencyPropertyByName(dependencyObject.GetType(), dpName);\n}\n\npublic static DependencyProperty GetDependencyPropertyByName(Type dependencyObjectType, string dpName)\n{\n    DependencyProperty dp = null;\n\n    var fieldInfo = dependencyObjectType.GetField(dpName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);\n    if (fieldInfo != null)\n    {\n        dp = fieldInfo.GetValue(null) as DependencyProperty;\n    }\n\n    return dp;\n}<\/pre>\n\n\n\n<p>The usage is simple.\u00a0 One method allows you to pass in the dependency object instance, plus the string representation of the DependencyProperty on the object.\u00a0 The other method allows you to get a dependency object from a Type.\u00a0 The most important thing to remember is the dpName parameter is the full name of the DependencyProperty as it is declared, not the property name you reference in code or XAML.\u00a0<\/p>\n\n\n\n<p>Consider the following example:<\/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=\"\">var usingInstance = GetDependencyPropertyByName(_button, \"ContentProperty\");\n\nvar usingType = GetDependencyPropertyByName(typeof(Button), \"ContentProperty\");<\/pre>\n\n\n\n<p>As you can see, the dpName parameter is the actual DependencyProperty name (ContentProperty) and not the property name (Content).\u00a0 Now that you have the DepedencyProperty, you can get all kinds of meta data about the property, and even creating dynamic data bindings.\u00a0 I hope this is helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever needed to create bindings to elements without knowing, or having access to, their DependencyProperty until runtime?<\/p>\n","protected":false},"author":43,"featured_media":2370,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-435","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/435","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=435"}],"version-history":[{"count":1,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/435\/revisions\/1691"}],"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=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}