2

我正在尝试将 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 的策略注入应用程序块使用策略注入。

我在运行时收到此错误:

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)'
4

4 回答 4

3

我不熟悉 entlib 的策略注入,但我很确定你的问题出在那儿,而不是因为你正在使用接口。
如果你要更换

var person = PolicyInjection.Wrap<IPerson>(new Person());

IPerson person = new Person();

肯定会奏效吗?

于 2008-09-16T21:28:52.153 回答
1

在我们的项目中,我们几乎只绑定了接口,一切都没有问题。您遇到的问题是由于 entlib... 但我对 entlib 不够熟悉,无法帮助您。但是,WPF 可以绑定到接口。

于 2008-09-16T21:36:48.377 回答
0

我认为代码没有太大问题。从技术上讲,您正在绑定 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 类型。

于 2008-09-16T21:33:25.543 回答
0

尝试在 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 数据绑定引擎只能与公共类型一起使用。

于 2012-01-27T22:13:56.093 回答