title: 'System.Windows.Data.Binding' is not a valid value for property 'SelectedIndex'
body: <p>Can someone please shed some light on this error?</p>
  
  <p>At first I thought SelectedIndex is probably just not a DependencyProperty and cannot be bound, but I was wrong.</p>
  
  <p>If I use a normal binding instead of the markup extension <code>src:ValidatedBinding</code>, or if I keep the markup extension but bind <code>SelectedItem</code> instead of <code>SelectedIndex</code>, then it works.</p>
  
  <p>Here is a small app to demonstrate the problem.</p>
  
  <p>The main window:</p>
  
  <pre><code>&lt;Window       x:Class="WpfApplication2.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:src="clr-namespace:WpfApplication2"
                  Title="MainWindow" 
                 Height="350" 
                  Width="525"
                       &gt;
      &lt;ComboBox SelectedIndex="{src:ValidatedBinding SelectedIndex}"
                VerticalAlignment="Center" HorizontalAlignment="Center" Width="100"&gt;
          &lt;ComboBoxItem&gt;Not Specified&lt;/ComboBoxItem&gt;
          &lt;ComboBoxItem&gt;First&lt;/ComboBoxItem&gt;
          &lt;ComboBoxItem&gt;Second&lt;/ComboBoxItem&gt;
      &lt;/ComboBox&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The code behind the main window:</p>
  
  <pre><code>using System.Windows;
  
  namespace WpfApplication2
  {
      /// &lt;summary&gt;
      /// The main window.
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              DataContext = new Item { Description = "Item 1", SelectedIndex = 0 };
          }
      }
  
      /// &lt;summary&gt;
      /// An object with a string and an int property.
      /// &lt;/summary&gt;
      public sealed class Item
      {
          int _selectedIndex;
          string _description;
  
          public string Description
          {
              get { return _description; }
              set { _description = value; }
          }
  
          public int SelectedIndex
          {
              get { return _selectedIndex; }
              set { _selectedIndex = value; }
          }
      }
  }
  </code></pre>
  
  <p>The code for the markup extension:</p>
  
  <pre><code>using System;
  using System.Windows;
  using System.Windows.Data;
  using System.Windows.Markup;
  
  namespace WpfApplication2
  {
      /// &lt;summary&gt;
      /// Creates a normal Binding but defaults NotifyOnValidationError and 
      /// ValidatesOnExceptions to True, Mode to TwoWay and UpdateSourceTrigger
      /// to LostFocus.
      /// &lt;/summary&gt;
      [MarkupExtensionReturnType(typeof(Binding))]
      public sealed class ValidatedBinding : MarkupExtension
      {
          public ValidatedBinding(string path)
          {
              Mode = BindingMode.TwoWay;
  
              UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
  
              Path = path;
          }
  
          public override object ProvideValue(IServiceProvider serviceProvider)
          {
              var Target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
  
              /* on combo boxes, use an immediate update and validation */
              DependencyProperty DP = Target.TargetProperty as DependencyProperty;
              if (DP != null &amp;&amp; DP.OwnerType == typeof(System.Windows.Controls.Primitives.Selector)
                  &amp;&amp; UpdateSourceTrigger == UpdateSourceTrigger.LostFocus) {
                  UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
              }
  
              return new Binding(Path) {
                  Converter = this.Converter,
                  ConverterParameter = this.ConverterParameter,
                  ElementName = this.ElementName,
                  FallbackValue = this.FallbackValue,
                  Mode = this.Mode,
                  NotifyOnValidationError = true,
                  StringFormat = this.StringFormat,
                  ValidatesOnExceptions = true,
                  UpdateSourceTrigger = this.UpdateSourceTrigger
              };
          }
  
          public IValueConverter Converter { get; set; }
  
          public object ConverterParameter { get; set; }
  
          public string ElementName { get; set; }
  
          public object FallbackValue { get; set; }
  
          public BindingMode Mode { get; set; }
  
          [ConstructorArgument("path")]
          public string Path { get; set; }
  
          public string StringFormat { get; set; }
  
          public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
      }
  }
  </code></pre>
  
  <p>The exception when I run this application:</p>
  
  <blockquote>
    <p>System.Windows.Markup.XamlParseException occurred<br>
    HResult=-2146233087   Message=<strong>'Set property
    'System.Windows.Controls.Primitives.Selector.SelectedIndex' threw an
    exception.</strong>' Line number '9' and line position '19'.<br>
    Source=PresentationFramework   LineNumber=9   LinePosition=19<br>
    StackTrace:
           at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean
    skipJournaledProperties, Object rootObject, XamlObjectWriterSettings
    settings, Uri baseUri)
           at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject,
    XamlAccessLevel accessLevel, Uri baseUri)
           at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
           at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
           at WpfApplication2.MainWindow.InitializeComponent() in c:\Users\Administrator\Documents\Visual Studio
    2012\Projects\WpfApplication2\MainWindow.xaml:line 1
           at WpfApplication2.MainWindow..ctor() in c:\Users\Administrator\Documents\Visual Studio
    2012\Projects\WpfApplication2\MainWindow.xaml.cs:line 12<br>
    InnerException: System.ArgumentException
           HResult=-2147024809
           Message=<strong>'System.Windows.Data.Binding' is not a valid value for property 'SelectedIndex'</strong>.
           Source=WindowsBase
           StackTrace:
                at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,
    Object value, PropertyMetadata metadata, Boolean
    coerceWithDeferredReference, Boolean coerceWithCurrentValue,
    OperationType operationType, Boolean isInternal)
                at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object
    value)
                at System.Windows.Baml2006.WpfMemberInvoker.SetValue(Object instance,
    Object value)
                at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member,
    Object obj, Object value)
                at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst,
    XamlMember property, Object value)
           InnerException:</p>
  </blockquote>