C# 类型为';的未处理异常;System.StackOverflowException';在WPF telerik TreeView控件的mscorlib.dll中发生
这是我的xamlC# 类型为';的未处理异常;System.StackOverflowException';在WPF telerik TreeView控件的mscorlib.dll中发生,c#,.net,wpf,xaml,telerik,C#,.net,Wpf,Xaml,Telerik,这是我的xaml <Window.Resources> <sampleData:MainWindow x:Key="DataSource"/> <DataTemplate x:Key="CustomComponentParameter"> <TextBlock Text="{Binding Name}" /> </DataTemplate> <HierarchicalDataTem
<Window.Resources>
<sampleData:MainWindow x:Key="DataSource"/>
<DataTemplate x:Key="CustomComponentParameter">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
<HierarchicalDataTemplate x:Key="CustomComponent" ItemTemplate="{StaticResource CustomComponentParameter}"
ItemsSource="{Binding Parameters }">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</Window.Resources>
每次运行它时,我都会收到以下错误“mscorlib.dll中发生类型为'System.StackOverflowException'的未处理异常”。无法计算表达式,因为当前线程处于堆栈溢出状态。
有什么建议吗?感谢堆栈溢出异常正在发生,因为您正在
主窗口的资源中创建的实例,执行
将程序发送到无限递归中
如果要将窗口的DataContext
设置为self,则删除这一行,并在main窗口的构造函数中执行该操作
public MainWindow()
{
InitializeComponents();
this.SummaryViewCollection = //code to fill in the details
DataContext = this;
}
然后你的装订就可以了
<telerik:RadTreeView ItemsSource="{Binding Path=SummaryViewCollection}" ItemTemplate="{StaticResource CustomComponent}" HorizontalAlignment="Left" Height="77" Margin="345,482,0,0" VerticalAlignment="Top" Width="449">
</telerik:RadTreeView>
在我的例子中,对DataBind()的额外调用导致了此异常。删除那个额外的调用修复了这个问题。为什么要设置ItemTemplate?我认为没有必要在您的代码中这样做。如果所有用于填写详细信息的//代码都被禁用,您仍然会得到异常吗?在某个地方,当您尝试启动时,您会遇到一些重复调用自身的情况。是否有堆栈跟踪,可以显示当您在调试器中捕获异常时,它在代码中的起始位置?需要知道哪一行引发该异常。在VS Debug中,抛出异常、CLR。然后在debug中运行它。如果错误不明显,请查看堆栈跟踪。@gleng您指的是哪个项目模板?您好。这解决了异常问题,但控件现在在UI中不可见。我们是否可以在codebehind中将treeview的datacontext设置为SummaryViewCollection(即使这样也不起作用)。:)您能检查一下输出窗口中是否有任何绑定错误吗?不,我没有看到任何绑定错误。只是树视图没有出现。我相信只有在装订正确的情况下才会出现。Hi@Nit对延迟表示歉意。ItemSource的值必须仅为{Binding},因为treeView的DataContext设置为codebehind中的集合。TreeView在codehind中控制自身。我已经更新了您的代码,并将批准它作为一个可接受的答案
public class CustomComponent
{
private ObservableCollection<CustomComponentParameter> parameters = new ObservableCollection<CustomComponentParameter>();
public string Name
{
get;
set;
}
public ObservableCollection<CustomComponentParameter> Parameters
{
get
{
return this.parameters;
}
set
{
this.parameters = value;
}
}
}
public class CustomComponentParameter
{
public string Name
{
get;set;
}
public string Value
{
get;set;
}
public bool HasErrors
{
get;set;
}
public bool IsDuplicate
{
get;set;
}
public bool IsMissing
{
get; set;
}
}
public MainWindow()
{
InitializeComponents();
this.SummaryViewCollection = //code to fill in the details
DataContext = this;
}
<telerik:RadTreeView ItemsSource="{Binding Path=SummaryViewCollection}" ItemTemplate="{StaticResource CustomComponent}" HorizontalAlignment="Left" Height="77" Margin="345,482,0,0" VerticalAlignment="Top" Width="449">
</telerik:RadTreeView>