title: UWP UserControl with its own ViewModel as well as Dependency Properties using Template 10
body: <p>I am creating UWP app using Template 10 and MVVM. My requirement is to create UserControl which will have its own dependency properties as well as has it own ViewModel.</p>
  
  <p>My Requirement:</p>
  
  <ol>
  <li>Call Parent ViewModel command on Button Click.</li>
  <li>Bind TexBlock text from UserControl ViewModel</li>
  </ol>
  
  <p>My User Control looks like this: </p>
  
  <pre><code>  &lt;vm:MyUserControl1  AddItem="{Binding MyCommand}"   Component="{Binding}"  RelativePanel.Below="abc" /&gt;
  </code></pre>
  
  <p>User Control XAML:</p>
  
  <pre><code>&lt;StackPanel&gt;
              &lt;TextBlock Text="{x:Bind Component.Text, Mode=OneWay}"/&gt;
              &lt;Button x:Name="Button" Content="Click Me" Command="{x:Bind AddItem}"&gt;
              &lt;/Button&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>This is UserControl code behind code :</p>
  
  <pre><code> public sealed partial class MyUserControl1 : UserControl
      {
          public MyUserControl1()
          {
              this.InitializeComponent();
              //   mygrid.DataContext = this;
              (this.Content as FrameworkElement).DataContext = this;
          }
  
          public static readonly DependencyProperty AddItemProperty =
            DependencyProperty.Register(
                "AddItem",
                typeof(ICommand),
                typeof(MyUserControl1), new PropertyMetadata(null));
  
          public ICommand AddItem
          {
              get { return (ICommand)GetValue(AddItemProperty); }
              set { SetValue(AddItemProperty, value); }
          }
  
          public static readonly DependencyProperty ComponentProperty = DependencyProperty.Register("Component",typeof(MyViewModel),typeof(MyUserControl1),new PropertyMetadata(null));
  
          public MyViewModel Component
          {
              get { return (MyViewModel)GetValue(ComponentProperty); }
              set { SetValue(ComponentProperty, value); }
          }
  
      }
  </code></pre>
  
  <p>UserControl View Model:</p>
  
  <pre><code>public class MyViewModel:ViewModelBase
      {
          public MyViewModel()
          {
  
          }
          public string Text =&gt; "ABC";
      }
  </code></pre>
  
  <p>Parent View Model:</p>
  
  <pre><code>  public class SettingsPartViewModel : ViewModelBase
    {
          DelegateCommand _MyCommand;
  
          public DelegateCommand MyCommand
              =&gt; _MyCommand ?? (_MyCommand = new DelegateCommand(async () =&gt;
              {
                  await Task.Run(() =&gt; {
                    ///Some Code  
                  });
              }));
       }
  </code></pre>
  
  <p>Whenever I run code I get following Error:</p>
  
  <pre><code> Unable to cast object of type 'WindowsApp2.ViewModels.SettingsPartViewModel' to type 'WindowsApp2.ViewModels.MyViewModel'. 
  </code></pre>
  
  <p>Whats going wrong here?</p>