Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnPropertyChange Firing Order

Tags:

c#

mvvm

wpf

xaml

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

ArgumentOutOfRangeException: MaxDateTime cannot be less then MinDateTime

This only occurs when I go from a later date to an earlier date. I am pretty sure that is due to the way the Properties are automatically updated to the view. This is the relevant XAML.

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

The ViewModel has this:

        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 a way around this problem? Is there a way to tell WPF which order the parameters within an object should be updated?

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

public MyObjectViewModel MyObject {
    get { return myObject; }
    set 
    {
        myObject= new MyObjectViewModel();
        OnPropertyChanged("MyObject");

        myObject= value;
        OnPropertyChanged("MyObject");    
    } 
}
like image 565
jle Avatar asked Aug 15 '12 15:08

jle


1 Answers

Could you try this:

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

                myObject= value;
                OnPropertyChanged("MyObject");
            }
        }
like image 140
CSmith Avatar answered Oct 05 '22 12:10

CSmith