{"id":9456,"date":"2022-09-05T18:13:44","date_gmt":"2022-09-05T18:13:44","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9456"},"modified":"2022-09-05T18:13:44","modified_gmt":"2022-09-05T18:13:44","slug":"wpf-usercontrol-dependencyproperty-binding","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/wpf-usercontrol-dependencyproperty-binding\/","title":{"rendered":"WPF UserControl DependencyProperty Binding"},"content":{"rendered":"<p>On the weekend I was messing around creating a simple WPF card game application and wanted to just quickly knock together a UserControl with a dependency property (actually there were several controls and several dependency properties) which allows the hosting Window or UserControl to change the property.<\/p>\n<p>Ofcourse we should probably look at implementing this as a <a href=\"https:\/\/putridparrot.com\/blog\/a-simple-lookless-control-in-wpf\/\" rel=\"noopener\" target=\"_blank\">lookless control<\/a> but I was just trying to do the bare minimum to get this working as a proof of concept.<\/p>\n<p>So what I want is UserControl which displays playing card, so let&#8217;s call it CardView and it looks like this (excluding the UserControl element for brevity)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;Grid&gt;\r\n   &lt;Border Background=&quot;White&quot; Padding=&quot;8&quot; CornerRadius=&quot;8&quot;&gt;\r\n      &lt;Image Source=&quot;{Binding CardImage, RelativeSource={RelativeSource AncestorType=UserControl}}&quot;\/&gt;\r\n      &lt;Border.Effect&gt;\r\n         &lt;DropShadowEffect \/&gt;\r\n      &lt;\/Border.Effect&gt;\r\n    &lt;\/Border&gt;\r\n&lt;\/Grid&gt;\r\n<\/pre>\n<p>and the code behind looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic partial class CardView : UserControl\r\n{\r\n   public static readonly DependencyProperty CardImageProperty = DependencyProperty.Register(\r\n      nameof(CardImage), typeof(Uri), typeof(CardView), new PropertyMetadata(default(Uri)));\r\n\r\n   public Uri CardImage\r\n   {\r\n      get =&gt; (Uri)GetValue(CardImageProperty);\r\n      set =&gt; SetValue(CardImageProperty, value);\r\n   }\r\n\r\n   public CardView()\r\n   {\r\n      InitializeComponent();\r\n   }\r\n}\r\n<\/pre>\n<p>The key thing here is that we have this code-behind dependency property CardImage, and in the XAML we want to show the image of the card that was set through the dependency property.<\/p>\n<p>If this was a lookless control we&#8217;d have set this binding up like this <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nRelativeSource={RelativeSource TemplatedParent}\r\n<\/pre>\n<p>but there&#8217;s no separation here of style and code, instead everything&#8217;s in the UserControl, so we have to use<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nRelativeSource={RelativeSource AncestorType=UserControl}\r\n<\/pre>\n<p>This will then bind the UserControl Image to the dependency property in code-behind and allow our host Window or UserControl to simply do the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;local:CardView Width=&quot;140&quot; Height=&quot;200&quot; \r\n   CardImage=&quot;..\/Resources\/HQ.png&quot;\/&gt;\r\n<\/pre>\n<p>For this example, the png&#8217;s will be stored in a Resources folder with their Build Action set to Resource.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On the weekend I was messing around creating a simple WPF card game application and wanted to just quickly knock together a UserControl with a dependency property (actually there were several controls and several dependency properties) which allows the hosting Window or UserControl to change the property. Ofcourse we should probably look at implementing this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-9456","post","type-post","status-publish","format-standard","hentry","category-wpf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9456"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9456\/revisions"}],"predecessor-version":[{"id":9465,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9456\/revisions\/9465"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}