我想在WPF中使用HierarchicalDataTemplate和TreeView控件。我創建了一個viewmodel類,但每次程序啓動時都會發生System.StackOverflowException。我不知道爲什麼會發生這種情況。當綁定HierarchicalDataTemplate時出現StackoverflowException
這裏是WPF標記:
<TreeView Grid.Row="2" ItemsSource="{Binding ImportTasks}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type VM:SyncToolViewModel}" ItemsSource="{Binding Path=ImportTasks}">
<Label Content="{Binding }"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type VM:VmImportTask}" ItemsSource="{Binding Path=ImportTasks.Tables}">
<Label Content="{Binding }"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
這裏是部分代碼:
public class SyncToolViewModel
{
public DBSyncToolLib.DataContext DataContext { get; set; }
private List<VmImportTask> _ImportTasks;
public List<VmImportTask> ImportTasks
{
get
{
if (_ImportTasks == null)
{
_ImportTasks = (from IT in DataContext.ImportTask
select
new VmImportTask()
{
DBImportTask = IT
}
).ToList();
}
return _ImportTasks;
}
}
}
public class VmImportTask
{
public DBSyncToolLib.Schema.ImportTask DBImportTask { get; set; }
public List<VmImportTaskTable> Tables
{
get
{
var L = (from B in DBImportTask.ImportTaskTable
select new VmImportTaskTable()
{
DBImportTaskTable = B
}).ToList();
return L;
}
}
public override string ToString()
{
return this.DBImportTask.Name;
}
}