title: UserControl DataContext defined at XAML page level
body: <p>I defined a DataContext for a UserControl at XAML page level as follows (the last line being the relevant one):</p>
  
  <pre><code>&lt;UserControl
    x:Class="Sample.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="using:Sample.Models"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" 
    d:DesignHeight="768"
    d:DesignWidth="1024"
    DataContext="data:TestDataCollection"&gt;
  </code></pre>
  
  <p>The aim is to be able to access the DataContext object in the page hosting the UserControl as follows, <strong>myUserControl</strong> being the <code>x:Name</code> for the UserControl.</p>
  
  <pre><code>TestDataCollection tdc = myUserControl.DataContext as TestDataCollection;
  </code></pre>
  
  <p>Everything is working fine with data binding and UI displaying and updating as expected on the UWP platform.</p>
  
  <p>The only one problem is that the above code line is not returning the expected DataContext object. In fact, myUserControl.DataContext during debugging shows a string with a value "data:TestDataCollection" (same as in the above XAML code) rather than an object of type <code>TestDataCollection</code>.</p>
  
  <p>Here is another strange thing: If I set the DataContext in codebehind as:</p>
  
  <pre><code>this.DataContext = new TestDataCollection();
  </code></pre>
  
  <p>the problem with be gone, i.e., <strong>(myUserControl.DataContext as TestDataCollection)</strong> returns the DataContext object as expected.</p>
  
  <p>What am I doing wrong in setting the page DataContext in XAML?</p>