System.StackOverflowException This makes no sense ... how to debug?

I am building a WPF application that uses the simple MVVM and EF architecture.

I see a strange problem if, if I try to set the datetime property, I get a System.StackOverflowException . If I do not set the datetime property, I get no exception.

Binding:

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

Public property:

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

Trying to get through it with the debugger does not work. I looked at everything, trying to understand what was happening ... any hints of what might be causing this?

This is the definition for the CriticalDate class,

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

The _criticalDate field is a private instance of the CriticalDate class. CriticalDate is a class created by EF from my DB schema. He himself is not a DateTime .

FINAL UPDATE

I still donโ€™t know what happened ... I tore off the offensive section (including the binding) and rewrote it from scratch. I donโ€™t know what I did differently, but now it works. I think this was due to the way the control element was configured ... if I had more time (silly terms), I would come back to see what it was, but so far it's a mystery.

Thanks for sharing my confusion, if only ever so briefly.

+7
c #
source share
3 answers

Try removing OnPropertyChanged("Date"); or change the binding mode to OneWayToSource .

From the documentation of UpdateSourceTrigger .

Bindings that are TwoWay or OneWayToSource listen for changes to the target property and propagate them back to the source. This is called a source update. Typically, these updates occur whenever the target property changes. This is normal for checkboxes and other simple controls, but usually it is not suitable for text fields. Updating after each keystroke can decrease performance, and denies the user the usual ability to backtrack and correct input errors before moving to a new value. Therefore, the default value of UpdateSourceTrigger for the Text property is LostFocus, not PropertyChanged.

I believe that OnPropertyChanged("Date"); in the Date_set method Date_set updates the user interface, which in turn calls the Date_set method Date_set , which completes the loop and calls the recursion.

0
source share

Edit:. The following is useless: the debugger does not display the call stack for. Instead, take a look at https://stackoverflow.com/a/166173/ ...


Set the Visual Studio debugger to a StackOverflowExceptions gap and view the column.

 Ctrl+Alt+E 

or

 Debug->Exceptions... 

Click Find and enter StackOverflow , then check the box in the Thrown column.

You probably have hundreds of recursive calls in the same method or set of methods.

If an exception occurs inside the library, you may need to disable "Only my code"

0
source share

Make sure you do not update the date when you do not need it, and break the feedback loop in your binding. I usually set this check around my setters:

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

All Articles