Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Can't Databind to an Interface?
    text
    copied!<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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload