title: Databinding to a method in WPF
body: <p>I am having trouble databinding a <code>TextBox.Text</code> property to a object's method. The idea is allowing the user to write in a <code>TextBox</code> a file name and then have a <code>TextBlock</code> output that file's extension.</p>
  
  <pre><code>class GetFileInfo
  {
      public string GetFileExtension(string fileName)
      {
          return Path.GetExtension(fileName);
      }
  }
  </code></pre>
  
  <p>Here is my XAML:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;ObjectDataProvider x:Key="getFileInfo" MethodName="GetFileExtension" ObjectType="{x:Type local:GetFileInfo}"&gt;
          &lt;ObjectDataProvider.MethodParameters&gt;
              &lt;sys:String&gt;abc.text&lt;/sys:String&gt;
          &lt;/ObjectDataProvider.MethodParameters&gt;
      &lt;/ObjectDataProvider&gt;
  &lt;/Window.Resources&gt;
  
  &lt;StackPanel&gt;
      &lt;TextBox Name="textBox1"&gt;
          &lt;TextBox.Text&gt;
              &lt;Binding Source="{StaticResource getFileInfo}" Path="MethodParameters[0]" BindsDirectlyToSource="True" UpdateSourceTrigger="PropertyChanged" /&gt;
          &lt;/TextBox.Text&gt;
      &lt;/TextBox&gt;
      &lt;TextBlock Name="textBlock1" Text="{Binding Source={StaticResource getFileInfo}}"/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>For some reason it is not doing anything. Anyknow could point out what may be the reasons?
  Here is what I see on the designer and when I run the application:</p>
  
  <p><a href="https://i.stack.imgur.com/GAHOw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GAHOw.png" alt="alt text"></a>
  </p>
  
  <p>And here is what happens when I try setting other text at run-time:</p>
  
  <p><a href="https://i.stack.imgur.com/EK3TW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EK3TW.png" alt="alt text"></a>
  
  Here is the error given by de debugger when trying to set other text at run-time:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=MethodParameters[0]; DataItem='ObjectDataProvider' (HashCode=2207369); target element is 'TextBox' (Name='textBox1'); target property is 'Text' (type 'String') ArgumentException:'System.ArgumentException: Object of type 'MS.Internal.Data.PropertyPathWorker+IListIndexerArg' cannot be converted to type 'System.Int32'.
       at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
       at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
       at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
       at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
       at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value)
       at System.Windows.Data.BindingExpression.UpdateSource(Object value)'</p>
  </blockquote>