Search code examples
data-bindingsilverlight-4.0wcf-ria-services

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. I pull MemberPackage objects from WCF RIA services and bind them to a DataGrid on the UI. 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.


Solution

  • The problem could be that when you get the MemberPackages in the service/factory, make sure you have the .Include("Package") as below:

    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.