I have an object called ‘SelectedDay’ I instantiate it like this:
Day SelectedDay = new Day(DateTime.Parse("01/01/0001"));
In Window_Loaded I set a listBox Datacontext to it:
lb_summary.DataContext = SelectedDay;
Later on in my app when a user clicks on a day in another ListBox, I pass it the new SelectedDay:
public void RefreshSummary(Day _selectedDay)
{
SelectedDay = _selectedDay;
}
Im expecting lb_summary.DataContext to become _selectedDay but nothing happens, SelectedDay is the same as _selectedDay but the lb_summary.DataContext is empty.
What am I missing?
EDIT:
this is the object structure (they do implement INotifyPropertyChanged, edited to keep breif):
public class Day : INotifyPropertyChanged
{
public string Title { get; set; }
public DateTime DayDate { get; set; }
public ObservableCollection<Gig> Gigs { get; set; }// gigs booked in a day_cell
}
public class Gig : INotifyPropertyChanged
{
// Properties of a gig
}
For now forget all the talk about MVVM and DependencyProperties and the like… we need to get some basics straigt here.
The reason your assignment doesnt work is simple: Your datacontext doesn’t change, it is SelectedDay that changes. Your Day class is a reference type, so the DataContext when assigned now points to your first SelectedDay object.
However later on you decide to change the assignment of SelectedDay to another Day object. This works, however it does not change the reference the DataContext object has.