[英]WPF Can't Databind to an Interface?
我试图将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 = "jdoe@live.com";
this.DataContext = person;
请注意,我用作数据源的类必须是一个实体,因为我通过entlib的Policy Injection Application Block使用Policy Injection。
我在运行时遇到此错误:
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)'
我不熟悉entlib的策略注入,但是我很确定您的问题就在这里,而不是因为您使用的是接口。
如果要更换
var person = PolicyInjection.Wrap<IPerson>(new Person());
同
IPerson person = new Person();
肯定会起作用吗?
除了项目中的接口外,我们几乎绑定任何东西,所有这些都没有问题。 您遇到的问题是由于entlib引起的...但是我对entlib不够了解,无法在此为您提供帮助。 但是,WPF可以绑定到接口。
我认为代码没有多大错误。 从技术上讲,您正在绑定Person类的实例(即无论如何尝试都没有必要绑定到接口)我不知道您的PolicyInjection.Wrap方法会做什么,但是我假设它返回了一个具体的人类? 无论如何,我只是尝试了一下,效果很好...
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
IPerson person = new Person() { FirstName = "Hovito" };
this.DataContext = person;
}
}
public class Person : IPerson
{
public virtual string FirstName { get; set; }
public string LastName { get; set; }
}
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set; }
}
我建议您多研究一下PolicyInjection类。 找出它是否确实返回了您所期望的Person类型。
尝试在XAML中显式指定属性路径:
<TextBox Name="userIdTextBox" Text="{Binding (myns:IPerson.UserId)}" />
<TextBox Name="userNameTextBox" Text="{Binding (myns:IPerson.UserName)}" />
<TextBox Name="emailTextBox" Text="{Binding (myns:IPerson.Email)}" />
我猜想策略注入生成的类型基于Person类,但是是动态的和内部的。 据我所知,XAML数据绑定引擎只能与公共类型一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.