title: WPF TextAlignmentProperty.OverrideMetadata not working in inheritor of TextBox
body: <p>I have created a custom control, which inherits TextBox. It basically has an extra property 'Seconds' and set a binding on 'Text', to shown the 'Seconds' formatted, as eg. 2m 5s, using a converter.</p>
  <p>I now want to default right-align the text.
  From other custom controls I know we sometimes will want to set/override values using styles. If I set the value directly in the constructor I will not be able to do this.</p>
  <p>I would usually something like this:</p>
  <pre><code>TextAlignmentProperty.OverrideMetadata(typeof(DurationTextBox), new FrameworkPropertyMetadata(TextAlignment.Right));
  </code></pre>
  <p>But this does NOT appear to work:</p>
  <p><a href="https://i.stack.imgur.com/Ew7Hv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ew7Hv.png" alt="First two have the default alignment, then they are Left, Center and Right aligned directly on the controls. Seconds row has a style setting alignment to Center" /></a></p>
  <p>First two have the default alignment, then they are Left, Center and Right aligned directly on the controls. Seconds row has a style setting alignment to Center</p>
  <p>I have bound a TextBlock to the TextAlignment of the first DurationTextBox, and this states that the aligment is 'Right', but this is not how it is shown!</p>
  <p>Can anyone explain:</p>
  <p><strong>A. Why this is not working?</strong></p>
  <p><strong>B. How to do this correctly, or something with the same end effect? (default aligned Right, but possible to override from Style)</strong></p>
  <p>C# class :</p>
  <p>Please note that this is a simplified version. The complete one has Min, Max, option of confirming value changed and option for out of range action, which is the reason for the structure of the class. Please keep focus on the TextAlignment issue!
  (The SecondsToDurationStringConverter and DurationStringValidator can be removed to make the example compile with the same effect)</p>
  <pre><code>public class DurationTextBox : TextBox
  {
      #region Dependency properties
  
      /// &lt;summary&gt;
      /// Property for &lt;see cref=&quot;Seconds&quot;/&gt;
      /// &lt;/summary&gt;
      [NotNull] public static readonly DependencyProperty SecondsProperty = DependencyProperty.Register(nameof(Seconds), typeof(double), typeof(Demo.DurationTextBox), new FrameworkPropertyMetadata(default(double), SecondsChangedCallback) { BindsTwoWayByDefault = true });
  
      /// &lt;summary&gt;
      /// Seconds to show as duration string
      /// &lt;/summary&gt;
      public double Seconds
      {
          // ReSharper disable once PossibleNullReferenceException
          get { return (double)GetValue(SecondsProperty); }
          set { SetValue(SecondsProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Property for &lt;see cref=&quot;EditValue&quot;/&gt;
      /// &lt;/summary&gt;
      [NotNull] public static readonly DependencyProperty EditValueProperty = DependencyProperty.Register(nameof(EditValue), typeof(double), typeof(Demo.DurationTextBox), new FrameworkPropertyMetadata(default(double), EditValueChangedCallback) { BindsTwoWayByDefault = true });
  
      /// &lt;summary&gt;
      /// Number being edited by the actual text box. Transferred to &lt;see cref=&quot;Seconds&quot;/&gt;.
      /// &lt;para&gt;Do NOT bind to this property from outside this control&lt;/para&gt;
      /// &lt;/summary&gt;
      public double EditValue
      {
          // ReSharper disable once PossibleNullReferenceException
          get { return (double)GetValue(EditValueProperty); }
          set { SetValue(EditValueProperty, value); }
      }
      #endregion Dependency properties
  
      private bool _isLocked;
  
      static DurationTextBox()
      {
          // TextAlignment
          TextAlignmentProperty.OverrideMetadata(typeof(Demo.DurationTextBox), new FrameworkPropertyMetadata(TextAlignment.Right));
      }
  
      /// &lt;inheritdoc /&gt;
      public DurationTextBox()
      {
          SecondsToDurationStringConverter secondsToDurationStringConverter = new SecondsToDurationStringConverter();
  
          // Text
          Binding binding = new Binding(nameof(EditValue)) { Source = this, Converter = secondsToDurationStringConverter, NotifyOnValidationError = true };
          binding.ValidationRules.Add(new DurationStringValidation());
          SetBinding(TextProperty, binding);
  
      }
  
      
  
      private static void SecondsChangedCallback([CanBeNull] DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          Demo.DurationTextBox durationTextBox = d as Demo.DurationTextBox;
          if (durationTextBox == null) return;
          if (!durationTextBox._isLocked)
          {
              durationTextBox._isLocked = true;
              durationTextBox.SetCurrentValue(EditValueProperty, durationTextBox.Seconds);
              durationTextBox._isLocked = false;
          }
      }
  
  
      private static void EditValueChangedCallback([CanBeNull] DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          Demo.DurationTextBox durationTextBox = d as Demo.DurationTextBox;
          if (durationTextBox == null) return;
          if (!durationTextBox._isLocked)
          {
              durationTextBox._isLocked = true;
              durationTextBox.SetCurrentValue(SecondsProperty, durationTextBox.EditValue);
              durationTextBox._isLocked = false;
          }
      }
  
    
  }
  </code></pre>
  <p>XAML code:</p>
  <pre><code>    &lt;Label Content=&quot;Demo.DurationTextBox&quot; FontWeight=&quot;Bold&quot;/&gt;
      &lt;WrapPanel&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; x:Name=&quot;DemoDurationTextBox&quot;/&gt;
          &lt;TextBlock Text=&quot;{Binding ElementName=DemoDurationTextBox, Path=TextAlignment}&quot;/&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot;  /&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; TextAlignment=&quot;Left&quot;/&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; TextAlignment=&quot;Center&quot;/&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; TextAlignment=&quot;Right&quot;/&gt;
      &lt;/WrapPanel&gt;
      &lt;WrapPanel&gt;
          &lt;WrapPanel.Resources&gt;
              &lt;Style TargetType=&quot;demo:DurationTextBox&quot;&gt;
                  &lt;Setter Property=&quot;TextAlignment&quot; Value=&quot;Center&quot;/&gt;
              &lt;/Style&gt;
          &lt;/WrapPanel.Resources&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot;/&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot;  /&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; TextAlignment=&quot;Left&quot; /&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; TextAlignment=&quot;Center&quot; /&gt;
          &lt;demo:DurationTextBox MinWidth=&quot;150&quot; Seconds=&quot;{Binding ElementName=Duration1, Path=Text}&quot; TextAlignment=&quot;Right&quot; /&gt;
      &lt;/WrapPanel&gt;
  </code></pre>