我有一个EntityFramework生成的DbSet,我从Grid.DataContext绑定到
myEntities = new MyEntities();
grMain.DataContext = myEntities.GalleryLists.ToList();
所有人都是华丽而花花公子,但List不是一个可观察的收藏品。我实际上(可能)想要这样做:
grMain.DataContext = myEntities.GalleryLists.Local; //myEntities.GalleryLists //???;
但后一个我想要工作的选项,在这个Grid中给了我空的ListBox,而前一个可以完美地工作。那么我做错了什么?
Grid使用ListBoxes以下列方式定义(设置用于绑定到Gallery列表):
<Grid Name="grMain">
<!-- Column definitions and such -->
<ListBox Name="lbxLists" Grid.Row="1" Grid.Column="0"
ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=GalleryName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Name="lbxItems" Grid.Row="1" Grid.Column="1"
ItemsSource="{Binding ElementName=lbxLists, Path=SelectedItem.GalleryItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=GalleryItemPath}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
实体框架生成的对象如下所示:
public partial class TiresiasEntities : DbContext
{
public TiresiasEntities()
: base("name=TiresiasEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<GalleryItem> GalleryItems { get; set; }
public virtual DbSet<GalleryList> GalleryLists { get; set; }
}
public partial class Gallery
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public GalleryList()
{
this.GalleryItems = new HashSet<GalleryItem>();
}
public int GalleryID { get; set; }
public string GalleryName { get; set; }
public byte IsSystemGallery { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<GalleryItem> GalleryItems { get; set; }
}
感谢您的任何建议。
答案 0 :(得分:1)
您可能需要在绑定到DbSet.Local之前调用IQueryable.Load():
myEntities = new MyEntities();
myEntities.GalleryLists.Load();
grMain.DataContext = myEntities.GalleryLists.Local;