{"id":102959,"date":"2019-10-03T07:00:00","date_gmt":"2019-10-03T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102959"},"modified":"2019-10-02T21:27:49","modified_gmt":"2019-10-03T04:27:49","slug":"20191003-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191003-00\/?p=102959","title":{"rendered":"What if I want the default value for a XAML dependency property to be a mutable object?"},"content":{"rendered":"<p>We saw last time that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191002-00\/?p=102950\"> the default value for a XAML dependency property should be immutable<\/a>. One easy case of an immutable object is <code>null<\/code>. But what if you want the default value to be a mutable object?<\/p>\n<p>There&#8217;s no perfect answer for this, but the common solution is to set an explicit value in your constructor.<\/p>\n<pre>class Light\r\n{\r\n  public Color Color { get; set; }; \/\/ read-write property\r\n}\r\n\r\nclass Widget\r\n{\r\n ...\r\n\r\n Widget()\r\n {\r\n  InitializeComponent();\r\n  <span style=\"color: blue;\">FrontLight = new Light() { Color = Colors.Red };<\/span>\r\n }\r\n\r\n public static readonly DependencyProperty FrontLightProperty =\r\n    DependencyProperty.Register(\"FrontLight\",\r\n    typeof(Light), typeof(Widget));\r\n\r\n \/\/ Provide convenient access to the dependency property.\r\n public Light FrontLight {\r\n  get =&gt; (Light)GetValue(FrontLightProperty);\r\n  set =&gt; SetValue(FrontLightProperty, value);\r\n }\r\n}\r\n<\/pre>\n<p>We define the dependency property with <code>null<\/code> as its default value, taking advantage of the overload that assumes that you&#8217;re okay with <code>null<\/code> being the default value.<\/p>\n<p>But in our constructor, we explicitly set the value of the <code>Front\u00adLight<\/code> property to a brand new red light. By explicitly setting a value (known in XAML terminology as <i>setting a local value<\/i>), we remove the case where XAML needs to produce a default value.<\/p>\n<p>This works out great for most purposes, but there are some subtleties.<\/p>\n<p>One is that <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/framework\/wpf\/advanced\/dependency-property-value-precedence#dependency-property-setting-precedence-list\"> the local value is fairly high in the precedence of value sources<\/a>, sitting above template properties, implicit styles, style triggers, template triggers, style setters, the default style, and inheritance. Setting a local value means that the dependency property cannot be styled, triggered, or inherited.<\/p>\n<p>Another is that somebody might try to reset the property back to the default by calling <code>Clear\u00adValue<\/code>. In that case, the value of the dependency property returns to the default value, which we implicitly declared as <code>null<\/code>, rather than returning to the initial value we set in our constructor (the red <code>Light<\/code>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Give it one, but save it for real.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-102959","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Give it one, but save it for real.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=102959"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102959\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=102959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}