MS SQL Server 中有 2 个表。你需要得到这样的树视图:
我正在使用实体框架从表中获取数据。上下文代码:
public partial class MSGContext : DbContext
{
public MSGContext()
: base("name=MSGContext")
{
}
public virtual DbSet<article> article { get; set; }
public virtual DbSet<section> section { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<article>()
.Property(e => e.articleName)
.IsFixedLength();
modelBuilder.Entity<section>()
.Property(e => e.sectionName)
.IsFixedLength();
modelBuilder.Entity<section>()
.HasMany(e => e.article)
.WithRequired(e => e.section)
.HasForeignKey(e => e.articleSection)
.WillCascadeOnDelete(false);
}
}
文章表型号:
[Table("article")]
public partial class article
{
public int articleId { get; set; }
[Required]
[StringLength(200)]
public string articleName { get; set; }
public int articleSection { get; set; }
[Required]
public string articleText { get; set; }
public byte[] articlePhoto { get; set; }
public virtual section section { get; set; }
}
截面表型号:
[Table("section")]
public partial class section
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public section()
{
article = new HashSet<article>();
}
public int sectionId { get; set; }
[Required]
[StringLength(100)]
public string sectionName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<article> article { get; set; }
}
如何使用 HierarchicalDataTemplate 填充树视图?
xml:
填充树视图: