DataContext not updating (newbie question)
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 not开发者_如何学JAVAhing 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.
As UrbanEsc said, your datacontext does not change. Hence setting the property SelectedDay is not gonna change anything or fire any propertychanges.
To make it work with the code you provided, you should write this:
public void RefreshSummary(Day _selectedDay)
{
lb_summary.DataContext = _selectedDay;
}
but as others pointed out, it would be better to set your DataContext to an object, and bind the listbox to a property of that object.
You generally don't bind to a value. You change the properties and implement INotifyPropertyChanged
.
May be you are to use Binding
? with UpdateSourceTrigger=PropertyChanged
and notify about property changed via INotifyPropertyChanged
interface?
Have a look at the MVVM design pattern. Here, you would create a view model type which contains the SelectedDay
property and implement INotifyPropertyChanged
on your view model. In the setter for your SelectedDay
property, you would invoke the PropertyChanged
event. You would then set the DataContext of your view to an instance of your view model.
If SelectedDay is not part of your ViewModel and might be a property of your control itself (for instance, a datepicker might have a SelectedDay property), consider making SelectedDay a DependencyProperty on your control, so it will handle the notification to the UI:
public static readonly DependencyProperty SelectedDayProperty =
DependencyProperty.Register("SelectedDay", typeof(Day), typeof(type_of_yourcontrol for instance DatePicker?));
public Day SelectedDay
{
get { return (Day)GetValue(SelectedDayProperty); }
set { SetValue(SelectedDayProperty, value); }
}
精彩评论