title: Access the value of an Attached property in a binding
body: <p>I am using attached properties to limit the input into a textbox.
  Now I would like to display the min and max limit in a tooltip but the tooltip either displays '0' for the two values or 'DependencyProperty.UnsetValue'. What's wrong with my bindings?</p>
  
  <pre><code>&lt;TextBox Text="{Binding Value}"
              Helper:InputService.InputMode="Numeric"
              Helper:InputService.Min="7"
              Helper:InputService.Max="{Binding Limit}"&gt;
              &lt;TextBox.ToolTip&gt;
                  &lt;TextBlock&gt;
                      &lt;TextBlock.Text&gt;
                          &lt;MultiBinding Converter="{converters:StringsToStringMultiConverter}" ConverterParameter=":" StringFormat="{}({0})"&gt;
                          &lt;Binding Path="(Helper:InputService.Min)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBox}}"/&gt;  
                          &lt;Binding Path="(Helper:InputService.Max)" RelativeSource="{RelativeSource Self}"/&gt; 
                          &lt;/MultiBinding&gt;
                      &lt;/TextBlock.Text&gt;
                  &lt;/TextBlock&gt;
              &lt;/TextBox.ToolTip&gt;
          &lt;/TextBox&gt;
  </code></pre>
  
  <p>(The Converter just appends the two values like (min:max)).
  For the Binding with {RelativeSource Self} I always get '0'.
  Searching for the parent element results in the 'UnsetValue'.</p>
  
  <p>I also would like to put the tooltip part into a text box style and use a trigger to check if the inputmode is numeric. </p>
  
  <p>Parts of the InputService:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The attached property for the input mode.
  /// &lt;/summary&gt;
  public static readonly DependencyProperty InputModeProperty = DependencyProperty.RegisterAttached(
    "InputMode",
    typeof (InputMode),
    typeof (InputService),
    new UIPropertyMetadata(InputMode.All, OnInputModeChanged));
  
  /// &lt;summary&gt;
  /// The attached property for checking the min limit of a number. Only applied if input mode is Numeric.
  /// &lt;/summary&gt;
  public static readonly DependencyProperty MinProperty = DependencyProperty.RegisterAttached(
    "Min",
    typeof (int),
    typeof (InputService));
  
  /// &lt;summary&gt;
  /// The attached property for checking the max limit of a number. Only applied if input mode is Numeric.
  /// &lt;/summary&gt;
  public static readonly DependencyProperty MaxProperty = DependencyProperty.RegisterAttached(
    "Max",
    typeof (int),
    typeof (InputService));
  
  
  private static void OnInputModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    var mode = (InputMode) e.NewValue;
  
    if (d is TextBox)
    {
      var textBox = (TextBox) d;
      var textPastedEventHandler = new RoutedEventHandler(OnTextPasted);
      textBox.AddHandler(CommandManager.PreviewExecutedEvent, textPastedEventHandler, true);
  
      if (mode == InputMode.Numeric)
      {
        textBox.PreviewTextInput += AllowNumbersOnly;
      }
      else
      {
        textBox.PreviewTextInput -= AllowNumbersOnly;
      }        
    }
  }
  </code></pre>
  
  <p>The min and max values are used in the AllowNumberOnly which composes the value from the input digits and checks if it exceeds one of the limits.</p>
  
  <p>EDIT:</p>
  
  <p>I simplified the code and removed the converter to be sure it's not the converter or the multibinding.</p>
  
  <pre><code>&lt;TextBox Text="{Binding Value}"
              Helper:InputService.InputMode="Numeric"
              Helper:InputService.Min="7"
              Helper:InputService.Max="{Binding Limit}"&gt;
              &lt;TextBox.ToolTip&gt;
                  &lt;TextBlock&gt;
                      &lt;TextBlock.Text&gt;
                          &lt;Binding Path="(Helper:InputService.Min)" RelativeSource="{RelativeSource Self}" StringFormat="{}({0})"/&gt;                             
                      &lt;/TextBlock.Text&gt;
                  &lt;/TextBlock&gt;
              &lt;/TextBox.ToolTip&gt;
          &lt;/TextBox&gt;
  </code></pre>
  
  <p>In the InputService I added default values to the min and max properties like this:</p>
  
  <pre><code>   public static readonly DependencyProperty MinProperty = DependencyProperty.RegisterAttached(
    "Min",
    typeof(int),
    typeof(InputService),
    new PropertyMetadata()
       {
         DefaultValue = 0
       });
  
     public static readonly DependencyProperty MaxProperty = DependencyProperty.RegisterAttached(
    "Max",
    typeof(int),
    typeof(InputService),
    new PropertyMetadata()
      {
        DefaultValue = int.MaxValue
      });
  </code></pre>
  
  <p>But still the tooltip shows '(0)'.</p>
  
  <p>EDIT:</p>
  
  <p>Ok, this one works:</p>
  
  <pre><code>&lt;TextBox Text="{Binding Value}"
              Helper:InputService.InputMode="Numeric"
              Helper:InputService.Min="7"
              Helper:InputService.Max="{Binding Limit}"&gt;
         &lt;TextBox.ToolTip&gt;
            &lt;MultiBinding Converter="{converters:StringsToStringMultiConverter}" ConverterParameter=":"&gt;
               &lt;Binding Path="(Helper:InputService.Min)" RelativeSource="{x:Static RelativeSource.Self}"/&gt;
               &lt;Binding Path="(Helper:InputService.Max)" RelativeSource="{x:Static RelativeSource.Self}"/&gt;
             &lt;/MultiBinding&gt;
         &lt;/TextBox.ToolTip&gt;
    &lt;/TextBox&gt;
  </code></pre>
  
  <p>I removed the TextBlock around the MultiBinding and the binding to relativesource self seems to work. What does not work anymore is the StringFormat. If defined in the MultiBinding, it will be ignored. If I define it as ContentStringFormat in a surrounding ToolTip, the bindings do not work anymore. 
  Any suggestions?</p>