8,735
社区成员
发帖
与我相关
我的任务
分享
namespace My_First_WPF.Model
{
public class Category
{
public Collection<Product> Products { get; set; }
}
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public Collection<Detail> Details { get; set; }
}
public class Detail
{
public DateTime Date { get; set; }
}
}
namespace My_First_WPF.ViewModel
{
public class ViewModel
{
private Model.Category category;
public ViewModel()
{
category = new Model.Category()
{
Products = new Collection<Model.Product>()
};
for (int i = 0; i < 10; i++)
{
Model.Product product = new Model.Product()
{
ID = i,
Name = "Name_" + i.ToString(),
Details = new Collection<Model.Detail>()
};
for (int j = 0; j < 3; j++)
{
Model.Detail detail = new Model.Detail()
{
Date = new DateTime(2015, 9, j + 1)
};
product.Details.Add(detail);
}
category.Products.Add(product);
}
}
public Model.Category Category
{
get { return this.category; }
}
}
}
namespace My_First_WPF.View
{
/// <summary>
/// Interaction logic for View.xaml
/// </summary>
public partial class View : Window
{
public View()
{
InitializeComponent();
this.DataContext = new ViewModel.ViewModel();
}
}
}
<TreeView Width="600">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Category}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>