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"); } }
source share