I am databinding an object value to a label and it wont refresh.
lblTime.DataBindings.Add(new Binding("Text", AppSettings.Instance.SelectedAuction, "EndDate", false, DataSourceUpdateMode.OnPropertyChanged));
The bind works and using a messagebox, I know the value is changing. I am correctly using the INotifyChanged but it wont work.
Changing individual values works, say:
AppSettings.Instance.SelectedAuction.EndDate = ((Auction)lbAuctions.SelectedItem).EndDate;
But I want to replace the whole object, and it wont update:
AppSettings.Instance.SelectedAuction = (Auction)lbAuctions.SelectedItem;
Why is this? I can make individual values refresh but not the object itself…
public Auction SelectedAuction
{
get { return this.selectedAuction; }
set
{
this.CheckPropertyChanged<Auction>
("SelectedAuction", ref this.selectedAuction, ref value);
}
}
Is it that there is another method to use when replacing the object itself or something additional i need to ref?
The data binding that is setup on
lblTimeis set on the object reference byAppSettings.Instance.SelectedAuctionat the time of the call toAddBinding. The databinding subscribes to thePropertyChangedevent on that object. Changing theSelectedAuctionon yourInstancedoesn’t change that. The data binding is still subscribed on the original object. (This also means you have a memory lead, since the data binding references the originalCurrentAuction, that instance will not be garbage collected)You need to instead setup the data binding so that it can listed for events on the
Instanceobject. You would have to set the binding to"CurrentAuction.EndDate". This will not work directly (* see note below), but there is a helper object, BindingSource, that can be put in the middle that will support that binding. Below is an example:Note: It will work without a
BindingSourcein 3.5, but not in 4.0, see Does data binding support nested properties in Windows Forms?