title: Binding WPF controls to dynamically introduced interfaces on model objects
body: <p>I am using PostSharp's [CompositionAspect] capability to dynamically inject an interface to a model object in my WPF application. However, it seems WPF cannot bind (display) properties corre   ctly unless the relevant interface is explicitly implemented on the model object? </p>
  
  <p>Using a custom CompositionAspect (ComposeObjectAspect) I am able to successfully expose the internal IMyInterface object's properties directly by runtime casting the ModelObject to the introduced interface;</p>
  
  <pre><code>// some interface definition, for sample completeness
  public interface IMyInterface 
  {
      public string MyProperty { get; set; }
  }
  
  
  // model object, which does NOT implement IMyInterface directly, though the interface is dynamically introduced by PostSharp
  [ComposeObjectAspect(typeof(IMyInterface))]
  public class ModelObject 
  {
      private IMyInterface actualDataObject;
  }
  
  . . .
  
  public class ViewModel
  {
      public ObservableCollection&lt;IMyInterface&gt; MyData { get; }
  
      public void LoadData()
      {
          // concrete IMyInterface dtos are returned here, and put into a VM collection
          IList&lt;IMyInterface&gt; myData = 
              (from IMyInterface o in SomeDataSource.LoadLocalDescriptors() 
               select new ModelObject(o) as IMyInterface).ToList();
          this.MyData = new ObservableCollection&lt;IMyInterface&gt;(myData);
      }
  }
  </code></pre>
  
  <p>This all works fine, and if I inspect any object in the myData list, or cast it to the type IMyInterface (which as you can see form the <strong>class ModelObject</strong> definition above is not implemented directly), I can see the inner properties from <strong>actualDataObject</strong> directly. This all works great.</p>
  
  <p>Now when I try to bind the ViewModel.Data collection to a WPF datagrid, I get a series of binding errors like;</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 40 : BindingExpression path error: 'SomeProperty' property not found on 'object' ''ModelObject' (HashCode=56810243)'. BindingExpression:Path=SomeProperty; DataItem='ModelObject' (HashCode=56810243); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')</p>
  </blockquote>
  
  <p>For reference, the relevant section of the View XAML looks like;</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Width="Auto" Header="Some Property" Binding="{Binding SomeProperty, Mode=OneWay}" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>It is the grid columns which are appearing blank, though all data rows are visible (and empty).</p>
  
  <p>If I inspect 'ModelObject', the property IMyInterface.SomeProperty is there. </p>
  
  <p>If I directly apply IMyInterface to the ModelObject class (and implenment all members as simple relays to the composed actualDataObject instance) then the binding is fine, and SomeProperty displays without error.</p>
  
  <p>What am I missing that is different between the PostSharp implementation of IMyInterface, and my own explicit version?</p>