title: XamlWriter.Save() is not serializing DependencyProperties
body: <p>Consider the following XAML from my UserControl:</p>
  <pre><code>&lt;TextBlock Text=&quot;HelloWorld&quot; Loaded=&quot;TextBlock_OnLoaded&quot; /&gt;
  </code></pre>
  <p>And the associated event handler:</p>
  <pre><code>private void TextBlock_OnLoaded(object sender, RoutedEventArgs e)
  {
      var xaml = XamlWriter.Save(sender);
      Console.WriteLine(xaml);
  }
  </code></pre>
  <p>When the TextBlock is loaded, the following output is written to the Console:</p>
  <pre><code>&lt;TextBlock Text=&quot;HelloWorld&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  </code></pre>
  <p>Now consider this alternative XAML:</p>
  <pre><code>&lt;ListBox ItemsSource=&quot;{Binding SomeCollection}&quot;&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock Text=&quot;HelloWorld&quot; Loaded=&quot;TextBlock_OnLoaded&quot; /&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  <p>Now when the TextBlock is loaded, the following output is written to the Console:</p>
  <pre><code>&lt;TextBlock xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  &lt;TextBlock xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  &lt;TextBlock xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  ......
  </code></pre>
  <p>Notice that the TextProperty is no longer being serialized.</p>
  <p>If the following TextProperty assignment is added before the call to XamlWriter.Save():</p>
  <pre><code>private void TextBlock_OnLoaded(object sender, RoutedEventArgs e)
  {
      var textBlock = sender as TextBlock;
      if (textBlock != null)
      {
          textBlock.Text = textBlock.Text;
      }
  
      var xaml = XamlWriter.Save(sender);
      Console.WriteLine(xaml);
  }
  </code></pre>
  <p>Then when the TextBlock is loaded, the following output is written to the Console:</p>
  <pre><code>&lt;TextBlock Text=&quot;HelloWorld&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  &lt;TextBlock Text=&quot;HelloWorld&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  &lt;TextBlock Text=&quot;HelloWorld&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; /&gt;
  ......
  </code></pre>
  <p>Notice that the TextProperty is again being serialized.</p>
  <p>This <a href="http://web.archive.org/web/20150810005407/http://blogs.msdn.com:80/b/mikehillberg/archive/2006/09/16/xamlwriter.aspx" rel="nofollow noreferrer">blog post</a> explains that &quot;... if the property is backed by a DependencyProperty ... the property is only written if it is actually set.&quot;</p>
  <p>It appears that the TextProperty is indeed being set in the first usage example, but not in the second usage example with the ListBox and DataTemplate.</p>
  <p>Can anyone explain why this is the case, and how to overcome this obstacle?</p>
  <p>My best guess is that the XAML parser is somehow setting the TextBlock state internally instead of calling SetValue on the dependency property, but I'm not sure why it would do this only for elements inside a DataTemplate.</p>