当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in WPF telerik TreeView Control

问题描述:

Here is my 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

 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

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

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

网友答案:

The stackoverflow exception is occuring because you are creating the instance of MainWindow in MainWindow's Resources by doing <sampleData:MainWindow x:Key="DataSource"/> which is sending the program into infinite recursion

If you want to set the DataContext of window to self then remove this line and in the constructor of your MainWindow do

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>
网友答案:

In my case, an extra call to DataBind() was causing this exception. Removing that extra call fixed the issue.

分享给朋友:
您可能感兴趣的文章:
随机阅读: