ObservableCollection, Databinding

Nate Bross

Well-known member
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
I have a timer that updates an observablecollection<ClassA> every user defined interval (30 seconds for example).

I have a Listbox databound to this collection, and pulling updates automatically works fine.

I have a textbox and a button, user selects a different item in list (currently using SelectedIndexChanged), I update the textbox.

Can I databind the textbox to a property of the currently selected item in my listbox?
 
I was able to resolve the issue above (Ill post a simplified solution soon). My next, related question is this.

How can I force the DataContext to refresh? This is what Im doing (please stop me if there is a better way)

Code:
ClassA obj = ((ClassA)listBox1.SelectedItem);
obj.Description = "New description";
DataAccessLayer.UpdateClassA(obj);

When I fire this code, my database is updated as expected, but the values in my ListBox are NOT updated until I manually refresh.

I thought that C# was "byref" by default, so updating as Ive done above should cause the ListBox items to refresh, no?

Please help!
 
Just as additional information, the method to update the database is async, so it returns immediatly; Ive also set the datacondext = null; and then back to my ObservableCollection and it is not reflecting my update.

Any thoughts?
 
I have not exhausted my testing to verify; but I believe I have found a solution. If I set the DataContext = new List<T>(0); and then to my modified datasource this causes the desired behavior; why DataContext = null; does not work is beyond me.

A bit more testing and Ill have confirmed that this is the "solution".
 
Turns out that I was fooled by the inconsistency of my problem; however, it still seems that it is ALWAYS one "update" behind. Any thoughts would really be helpful. Thanks.
 
Back
Top