title: WPF UserControl Dependency Property not working
body: <p>In a WPF project (code below) I have a <code>UserControl</code> of type <code>MyUserControl</code> with a dependency property, called <code>MyOrientation</code> of type <code>Orientation</code>.</p>
  
  <p>On the <code>MainWindow</code> I have 2 instances of <code>MyUserControl</code>, where via XAML I set the <code>Orientation</code> property on one to be <code>Horizontal</code> and the other instance to be <code>Vertical</code>.</p>
  
  <p>I have made the <code>MyOrientation</code> property a DP as I want the ability to set it directly in XAML as in this example or using a binding.</p>
  
  <p>My problem is that when I run the project both instances of the UserControl show up with the Orientation = Horizontal?</p>
  
  <p>Could someone please tell me what I am doing wrong and how to fix it?</p>
  
  <p>Many thanks in advance.</p>
  
  <p>Here is the code:</p>
  
  <p>MYUSERCONTROLVIEWMODEL:</p>
  
  <pre><code>public class MyUserControlViewModel : ViewModelBase
  {
      private Orientation _myOrientation;
      public Orientation MyOrientation
      {
          get { return _myOrientation; }
          set
          {
              if (_myOrientation == value)
                  return;
              _myOrientation = value;
              OnPropertyChanged();
          }
      }
  
  }
  </code></pre>
  
  <p>MYUSERCONTROL.XAML</p>
  
  <pre><code>&lt;UserControl x:Class="TestUserControlDPProblem.MyUserControl"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           xmlns:local="clr-namespace:TestUserControlDPProblem"
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"&gt;
  &lt;Grid x:Name="root"&gt;
  
      &lt;Grid.DataContext&gt;
          &lt;local:MyUserControlViewModel/&gt;
      &lt;/Grid.DataContext&gt;
  
      &lt;StackPanel Orientation="{Binding MyOrientation}"&gt;
          &lt;TextBlock&gt;Hello&lt;/TextBlock&gt;
          &lt;TextBlock&gt;There&lt;/TextBlock&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MYUSERCONTROL CODE BEHIND:</p>
  
  <pre><code>public partial class MyUserControl : UserControl
  {
      MyUserControlViewModel _vm;
  
      public MyUserControl()
      {
          InitializeComponent();
  
          _vm = root.DataContext as MyUserControlViewModel;
      }
  
      public static readonly DependencyProperty MyOrientationProperty = DependencyProperty.Register("MyOrientation", typeof(Orientation), typeof(MyUserControl), new FrameworkPropertyMetadata(Orientation.Vertical, new PropertyChangedCallback(OnMyOrientationChanged)));
  
      private static void OnMyOrientationChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
      {
          var myUserControl = o as MyUserControl;
          myUserControl?.OnMyOrientationChanged((Orientation)e.OldValue, (Orientation)e.NewValue);
      }
  
      protected virtual void OnMyOrientationChanged(Orientation oldValue, Orientation newValue)
      {
          _vm.MyOrientation = newValue;
      }
      public Orientation MyOrientation
      {
          get
          {
              return (Orientation)GetValue(MyOrientationProperty);
          }
          set
          {
              SetValue(MyOrientationProperty, value);
          }
      }
  
  }
  </code></pre>
  
  <p>MAINWINDOW.XAML</p>
  
  <pre><code>&lt;Window x:Class="TestUserControlDPProblem.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:TestUserControlDPProblem"
      mc:Ignorable="d"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;StackPanel&gt;
          &lt;local:MyUserControl Margin="10" MyOrientation="Horizontal"/&gt;
          &lt;local:MyUserControl Margin="10" MyOrientation="Vertical"/&gt;
      &lt;/StackPanel&gt;
  
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>