c# – System.StackOverflowException这没有意义……如何调试?

我正在构建一个使用简单MVVM架构和EF的 WPF应用程序.

我看到一个奇怪的问题,如果我尝试设置datetime属性,我得到一个System.StackOverflowException.如果我没有设置datetime属性,我不会得到异常.

捆绑:

<DatePicker Style="{StaticResource Dp}" 
               Grid.Column="1" 
               SelectedDate="{Binding Date,UpdateSourceTrigger=PropertyChanged}" />

公共财产:

public DateTime Date
{
    get
    {
        return _criticalDate.Date;
    }
    set
    {
        if (_criticalDate != null && value != null && _criticalDate.Date == value)
            return;
        _criticalDate.Date = value;
        OnPropertyChanged("Date");
    }
}

尝试使用调试器逐步执行它似乎不起作用.我已经看过所有试图看到发生了什么的事情……有什么迹象表明可能导致这种情况发生了什么?

这是CriticalDate类的定义,

public partial class CriticalDate
{
    public int ID { get; set; }
    public System.DateTime Date { get; set; }
    public string CriticalReason { get; set; }
    public int FileID { get; set; }
}

_criticalDate字段是CriticalDate类的私有实例. CriticalDate是EF从我的数据库模式创建的类.它本身不是DateTime.

最终更新

我仍然不知道出了什么问题……我撕掉了有问题的部分(包括绑定)并从头开始重写.不知道我做了什么不同,但它现在有效.我认为这与物品控制的设置方式有关…如果我有更多的时间(愚蠢的截止日期),我会回去看看它是什么,但现在这一个谜.

感谢您分享我的困惑,如果只是如此简短.

解决方法

尝试删除OnPropertyChanged(“Date”);或将绑定模式更改为OneWayToSource.

documentation of UpdateSourceTrigger.

Bindings that are TwoWay or OneWayToSource listen for changes in the target property and propagate them back to the source. This is kNown as updating the source. Usually,these updates happen whenever the target property changes. This is fine for check Boxes and other simple controls,but it is usually not appropriate for text fields. Updating after every keystroke can diminish performance and it denies the user the usual opportunity to backspace and fix typing errors before committing to the new value. Therefore,the default UpdateSourceTrigger value of the Text property is LostFocus and not PropertyChanged.

我相信OnPropertyChanged(“日期”);在Date_set方法上更新UI,然后再次调用Date_set方法,完成循环并导致递归.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...