WPF Telerik TreeView 控件的 mscorlib.dll 中发生类型为“System.StackOverflowException”的未处理异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19429932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in WPF telerik TreeView Control
提问by Sike12
Here is my xaml
这是我的 xaml
<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>
for the telerik control
用于电讯控制
<telerik:RadTreeView ItemsSource="{Binding Source={StaticResource DataSource},Path=SummaryViewCollection}" ItemTemplate="{StaticResource CustomComponent}" HorizontalAlignment="Left" Height="77" Margin="345,482,0,0" VerticalAlignment="Top" Width="449">
</telerik:RadTreeView>
Here is my Codebehind classes
这是我的代码隐藏类
Code for the main Codebehind Class MainWindow.xaml.cs
主代码隐藏类 MainWindow.xaml.cs 的代码
public partial class MainWindow : Window
{
public ObservableCollection<CustomComponent> SummaryViewCollection { get; set; }
public MainWindow()
{
this.SummaryViewCollection = //code to fill in the details
}
}
Here is the code for CustomComponentClass
这是 CustomComponentClass 的代码
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;
}
}
}
Code for the CustomComponentParameter Class
CustomComponentParameter 类的代码
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;
}
}
Everytime i run it i get the following error "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll". Cannot evaluate expression because the current thread is in a stack overflow state. Any suggestion on this? Thanks
每次我运行它时,我都会收到以下错误“mscorlib.dll 中发生类型为‘System.StackOverflowException’的未处理异常”。无法计算表达式,因为当前线程处于堆栈溢出状态。对此有何建议?谢谢
回答by Nitin
The stackoverflow exception is occuring because you are creating the instance of MainWindowin MainWindow'sResourcesby doing <sampleData:MainWindow x:Key="DataSource"/>which is sending the program into infinite recursion
发生 stackoverflow 异常是因为您正在通过将程序发送到无限递归中来创建MainWindowin的实例MainWindow'sResources<sampleData:MainWindow x:Key="DataSource"/>
If you want to set the DataContextof window to self then remove this line and in the constructor of your MainWindowdo
如果要将DataContext窗口设置为 self 则删除此行并在MainWindowdo的构造函数中
public MainWindow()
{
InitializeComponents();
this.SummaryViewCollection = //code to fill in the details
DataContext = this;
}
and then your binding will be just
然后你的绑定将只是
<telerik:RadTreeView ItemsSource="{Binding Path=SummaryViewCollection}" ItemTemplate="{StaticResource CustomComponent}" HorizontalAlignment="Left" Height="77" Margin="345,482,0,0" VerticalAlignment="Top" Width="449">
</telerik:RadTreeView>
回答by salman
In my case, an extra call to DataBind() was causing this exception. Removing that extra call fixed the issue.
就我而言,对 DataBind() 的额外调用导致了此异常。删除那个额外的电话解决了这个问题。

