<p>I'm trying to bind controls in a WPF form to an interface and I get a runtime error that it can't find the interface's properties.</p>

<p>Here's the class I'm using as a datasource:</p>

<pre><code>public interface IPerson
{
    string UserId { get; set; }
    string UserName { get; set; }
    string Email { get; set; }
}

public class Person : EntityBase, IPerson
{
    public virtual string UserId { get; set; }
    public string UserName { get; set; }
    public virtual string Email { get; set; }
}
</code></pre>

<p>Here's the XAML (an excerpt):</p>

<pre><code>&lt;TextBox Name="userIdTextBox" Text="{Binding UserId}" /&gt;
&lt;TextBox Name="userNameTextBox" Text="{Binding UserName}" /&gt;
&lt;TextBox Name="emailTextBox" Text="{Binding Email}" /&gt;
</code></pre>

<p>Here's the code behind (again, an excerpt):</p>

<pre><code>var person = PolicyInjection.Wrap&lt;IPerson&gt;(new Person());
person.UserId = "jdoe";
person.UserName = "John Doe";
person.Email = "jdoe@live.com";

this.DataContext = person;
</code></pre>

<p>Note that the class I'm using as the data source needs to be an entity because I'm using Policy Injection through the entlib's Policy Injection Application Block.</p>

<p>I'm getting this error at runtime:</p>

<pre><code>System.Windows.Data Error: 16 : Cannot get 'Email' value (type 'String') from '' (type 'Person'). BindingExpression:Path=Email; DataItem='Person' (HashCode=22322349); target element is 'TextBox' (Name='emailTextBox'); target property is 'Text' (type 'String') TargetException:'System.Reflection.TargetException: Object does not match target type.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   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.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)
   at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'
</code></pre>

[{'answer_id': 77322, 'author': 'Robert Jeppesen', 'author_id': 9436, 'author_profile': 'https://Stackoverflow.com/users/9436', 'pm_score': 3, 'selected': True, 'text': "<p>I'm not familiar with entlib's policy injection, but I'm pretty sure that your problem lies there, and not in the fact that you're using an interface.<br>\nIf you were to replace</p>\n\n<pre><code>var person = PolicyInjection.Wrap&lt;IPerson&gt;(new Person());\n</code></pre>\n\n<p>with  </p>\n\n<pre><code>IPerson person = new Person();\n</code></pre>\n\n<p>surely it would work?</p>\n"}, {'answer_id': 77356, 'author': 'Senkwe', 'author_id': 6419, 'author_profile': 'https://Stackoverflow.com/users/6419', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t see much wrong with the code. Technically you\'re binding an instance of the Person class (ie it doesn\'t make sense to try and bind to an interface anyway) I don\'t know what your PolicyInjection.Wrap method does, but I\'m assuming it returns a concrete Person class? Anyway, I just tried this on my end and it works fine...</p>\n\n<pre><code>public partial class Window1 : Window\n{\n    public Window1()\n    {\n        InitializeComponent();\n\n        IPerson person = new Person() { FirstName = "Hovito" };\n\n        this.DataContext = person;\n    }\n}\n\npublic class Person : IPerson\n{\n    public virtual string FirstName { get; set; }\n    public string LastName { get; set; }\n}\n\npublic interface IPerson\n{\n    string FirstName { get; set; }\n    string LastName { get; set; }\n}\n</code></pre>\n\n<p>I would suggest you look into that PolicyInjection class a bit more. Find out if it really does return a Person type as you expect.</p>\n'}, {'answer_id': 77391, 'author': 'cranley', 'author_id': 10308, 'author_profile': 'https://Stackoverflow.com/users/10308', 'pm_score': 1, 'selected': False, 'text': "<p>We bind to almost nothing but Interfaces in our project, all without problem.  The problem you're experiencing is due to entlib... but I'm not familiar enough with entlib to help you there.  WPF can, however, bind to Interfaces.</p>\n"}, {'answer_id': 9040770, 'author': 'Philipp Munin', 'author_id': 508797, 'author_profile': 'https://Stackoverflow.com/users/508797', 'pm_score': 0, 'selected': False, 'text': '<p>Try to specify property path explicitly in your XAML:</p>\n\n<pre><code>&lt;TextBox Name="userIdTextBox" Text="{Binding (myns:IPerson.UserId)}" /&gt; \n&lt;TextBox Name="userNameTextBox" Text="{Binding (myns:IPerson.UserName)}" /&gt; \n&lt;TextBox Name="emailTextBox" Text="{Binding (myns:IPerson.Email)}" /&gt; \n</code></pre>\n\n<p>I guess the type generated by policy injection is based on Person class, but is dynamic and internal. As far as I know XAML data binding engine can work only with public types.</p>\n'}]
['https://Stackoverflow.com/questions/75713', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6542/']