0
我有一個有子對象的對象,我用linq加載它。我將它分配給一個treeView的itemssource。wpf treeview不顯示子對象
treeView.DisplayMemberPath = "Name";
treeView.ItemsSource = tasks;
它只顯示父節點(task.name),我找不出如何添加子項(TaskItems.name)。 所有示例都在xaml中顯示HierarchicalData。我需要在代碼隱藏方面做到這一點,就像上面的代碼一樣。可能嗎?
public class Task
{
public int Id;
public string Name;
public bool IsActive;
public List<TaskItem> TaskItems = new List<TaskItem>();
}
public class TaskItem
{
public int TaskId;
public string Name;
public string Value;
}
--------------
var tasks1 = from t in xd.Descendants("taskheader")
select new Task
{
Id = (int)t.Element("id"),
Name = t.Element("name").Value,
IsActive = t.Element("isactive").Value == "1",
TaskItems = t.Elements("taskdetail").Select(e => new TaskItem
{
TaskId = (int)e.Element("taskid"),
Name = (string)e.Element("name"),
Value = (string)e.Element("value"),
}).ToList()
};
--------------
List<Task> tasks = new List<Task>();
tasks = tasks1;