私はインターフェイスにWPFフォーム内のコントロールをバインドしようとしていると私はそれがインタフェースのプロパティを見つけることができないことを、実行時エラーが発生します。ここでWPFはインターフェイスにデータバインドできませんか?
は、私がデータソースとして使用していたクラスです:
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; }
}
はここでXAML(抜粋)です。
<TextBox Name="userIdTextBox" Text="{Binding UserId}" />
<TextBox Name="userNameTextBox" Text="{Binding UserName}" />
<TextBox Name="emailTextBox" Text="{Binding Email}" />
ここで(再び、抜粋)の背後にあるコードは次のとおりです。
私は、データソースとして使用していたクラスは、私はトンを通じてポリシーインジェクションを使用していますので、エンティティである必要がvar person = PolicyInjection.Wrap<IPerson>(new Person());
person.UserId = "jdoe";
person.UserName = "John Doe";
person.Email = "[email protected]";
this.DataContext = person;
注意彼はentlibのポリシー注入アプリケーションブロック。
私は、実行時にこのエラーを取得しています:
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)'
@Robertジェプセン:彼のPolicyInjectionコードを実行することはできません。 – user7116
ロバートが正しい - 問題は、WPFデータバインディングがPolicyInjection.Wrapが作成するプロキシクラスのプロパティを認識しないことです。データバインディングはPIなしで正常に動作するので、使用しないで回避する必要があります。 –