【问题标题】:An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in WPF telerik TreeView ControlWPF Telerik TreeView 控件的 mscorlib.dll 中发生了“System.StackOverflowException”类型的未处理异常
【发布时间】:2013-10-17 14:45:23
【问题描述】:

这是我的 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>

用于 Telerik 控制

    <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>

这是我的代码隐藏类

主代码隐藏类 MainWindow.xaml.cs 的代码

 public partial class MainWindow : Window
 {
    public ObservableCollection<CustomComponent> SummaryViewCollection { get; set; }
    public MainWindow()
    {          
       this.SummaryViewCollection = //code to fill in the details 
    }           
 }

这是自定义组件类的代码

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;
        }
    }
}

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;
    }
}

每次我运行它时,我都会收到以下错误“mscorlib.dll 中发生'System.StackOverflowException' 类型的未处理异常”。无法计算表达式,因为当前线程处于堆栈溢出状态。 对此有何建议?谢谢

【问题讨论】:

  • 为什么要设置 ItemTemplate?我认为在您的代码中不需要这样做。
  • 如果所有//code to fill in the details 都被禁用,你还会得到异常吗?当您尝试启动时,您会在某个地方反复调用自己。
  • 当您在调试器中捕获异常时,是否有堆栈跟踪向我们显示它在代码中的起始位置?
  • 需要知道哪一行抛出了异常。在 VS 调试中,异常,CLR,抛出。然后只需在调试中运行它。如果错误不明显,请查看堆栈跟踪。
  • @gleng 你指的是哪个项目模板?

标签: c# .net wpf xaml telerik


【解决方案1】:

发生 stackoverflow 异常是因为您正在通过执行 &lt;sampleData:MainWindow x:Key="DataSource"/&gt;MainWindow's Resources 创建 MainWindow 的实例,这会将程序发送到无限递归

如果你想将 window 的 DataContext 设置为 self 然后删除这一行并在你的 MainWindow 的构造函数中做

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>

【讨论】:

  • 嗨,尼特。这解决了异常问题,但控件现在在 UI 中不可见。我们不能做一些事情,比如在代码隐藏中将树视图的数据上下文设置为 SummaryViewCollection(即使这不起作用)。 :)
  • 你能检查一下你是否在输出窗口中遇到任何绑定错误吗?
  • 不,我没有看到任何绑定错误。它只是树视图没有出现。我相信它只有在绑定正确的情况下才会出现。
  • 嗨@Nit 为延迟道歉。 ItemSource 的 Value 只需 {Binding} 因为 treeView 的 DataContext 设置为代码隐藏中的集合。代码隐藏中的 treeeView 控件本身。我已经更新了您的代码,并将其批准为可接受的答案
【解决方案2】:

就我而言,对 DataBind() 的额外调用导致了此异常。删除那个额外的调用解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多