OnPropertyChange Firing Order

I have an application in which you can choose between different objects in a ListBox. When you select an object, it changes the view mode for the control. The control uses the Timeline Control from CodePlex, and because of this, I have StartDate and EndDate for timeline data bound to the ViewModel. When the ViewModel is changed, I sometimes get an error message:

ArgumentOutOfRangeException: MaxDateTime cannot be less then MinDateTime 

This only happens when I switch from a later date to an earlier date. I am sure that this is due to the fact that the properties are automatically updated in the view. This is the corresponding XAML.

 MaxDateTime="{Binding Path=RecordingEnd}" MinDateTime="{Binding Path=RecordingStart}" CurrentDateTime="{Binding Path=CurrentDateTime, Mode=TwoWay}" 

ViewModel has the following:

  private int myObjectIndex; public int MyObjectIndex { get { return myObjectIndex; } set { myObjectIndex = value; OnPropertyChanged("MyObjectIndex"); MyObject = MyObjects[myObjectIndex]; } } private MyObjectViewModel myObject=new MyObjectViewModel(); public MyObjectViewModel MyObject { get { return myObject; } set { myObject= value; OnPropertyChanged("MyObject"); } } 

Is there any way to solve this problem? Is there a way to tell WPF that orders parameters inside an object to be updated?

Update: I ended up using a variation of @ colinsmith's answer:

 public MyObjectViewModel MyObject { get { return myObject; } set { myObject= new MyObjectViewModel(); OnPropertyChanged("MyObject"); myObject= value; OnPropertyChanged("MyObject"); } } 
+6
source share
2 answers

Could you try:

 public MyObjectViewModel MyObject { get { return myObject; } set { myObject=null; OnPropertyChanged("MyObject"); myObject= value; OnPropertyChanged("MyObject"); } } 
+1
source

You can either handle the fact that the time in minutes can be updated to the maximum time and make something invalid (i.e., cancel the max or min setting until both max and min are updated). This can be very error prone. Or you can make max and min the same value so that they can be updated at the same time. for example, instead of RecordingEnd and RecordingStart they have RecordingSpan , which have both max and min in it.

0
source

Source: https://habr.com/ru/post/922953/


All Articles