简体   繁体   English

Silverlight数据绑定不起作用

[英]Silverlight databinding doesn't work

Below you can see a part of my class definitions: 在下面,您可以看到我的课程定义的一部分:

public class Package {
  public int PackageId { get; set; }
  public string Name { get; set; }
}

public class Member {
  public int MemberId { get; set; }
  public string DisplayName { get; set; }
}

public class MemberPackage {
  public int PackageId { get; set; }
  public int MemberId { get; set; }
  public DateTime DateSold { get; set; }

  public Member Member { get; set; }
  public Package Package { get; set; }
}

These are EF 4 model classes. 这些是EF 4模型类别。 I pull MemberPackage objects from WCF RIA services and bind them to a DataGrid on the UI. 我从WCF RIA服务中提取MemberPackage对象,并将其绑定到UI上的DataGrid。 To show the package names I use a binding syntax shown below: 为了显示包名,我使用如下所示的绑定语法:

<sdk:DataGridTextColumn Header="Package Name" Binding="{Binding Path=Package.Name}" />
<sdk:DataGridTextColumn Header="Date Sold" Binding="{Binding DateSold}" />

Nothing comes up under the Package Name column but I can see the Date Sold values. “包裹名称”列下没有任何显示,但是我可以看到“售出日期”值。 What's going on here, doesn't it supposed to work this way? 这是怎么回事,难道不应该这样工作吗?

Thanks in advance. 提前致谢。

这与视图的数据上下文有关-您确定Package是视图模型(数据上下文)的属性,并且这些模型实现INotifyPropertyChanged吗?

Simply remove the Path= and have this: 只需删除Path=并执行以下操作:

<sdk:DataGridTextColumn Header="Package Name" Binding="{Binding Package.Name}" />

You don't need to specify the path. 您无需指定路径。

If that doesn't work then you should need to make sure that the Package member of MemberPackage has the [Include] attribute in the server side definition. 如果这不起作用,则您需要确保MemberPackagePackage成员在服务器端定义中具有[Include]属性。 This will ensure that the whole hierarchy is serialised to the client. 这将确保将整个层次结构序列化到客户端。 I didn't suggest this at first as I assume that your code was edited. 我最初并不建议这样做,因为我假设您的代码已被编辑。

The problem could be that when you get the MemberPackages in the service/factory, make sure you have the .Include("Package") as below: 问题可能是当您在服务/工厂中获得MemberPackages时,请确保您具有.Include(“ Package”),如下所示:

return this.ObjectContext.MemberPackages
                .Include("Package");

This should bring back the Package details as part of the MemberPackage and then your binding to Package.Name should work. 这应该带回Package的详细信息作为MemberPackage的一部分,然后您对Package.Name的绑定应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM