c# - Bound property in WPF doesn't update -
i have textblock in wpf/mvvm (with mvvm light framework) game bound property supposed reflect number of employed workers. have confirmed binding intact, can't update.
here's textblock in view:
<textblock x:name="workerstextblock" fontfamily="pericles" datacontext="{binding guilds[0]}" text="{binding workers.count, stringformat=workers : {0}, fallbackvalue=workers : 99}" /> the property in viewmodel:
public observablecollection<guild> guilds { { return datamanager.data.guilds; } } also in viewmodel, command change worker's employer property:
private void executehireworkercommand() { if (selectedworker == null) return; selectedworker.employer = datamanager.data.guilds[0]; gold -= selectedworker.salary; _workercollectionview.refresh(); } in datamanager, singleton class holds of data:
private observablecollection<guild> _guilds = new observablecollection<guild>(); public observablecollection<guild> guilds { { return _guilds; } } private observablecollection<worker> _workers = new observablecollection<worker>(); public observablecollection<worker> workers { { return _workers; } } in guild model:
public observablecollection<worker> workers { { return datamanager.data.workers.where(w => w.employer == this).toobservablecollection(); } } the employer property in worker is:
public guild employer { get; set; } and last, extension method (which believe source of problem):
public static observablecollection<t> toobservablecollection<t>(this ienumerable<t> source) { if (source == null) { throw new argumentnullexception("source"); } return new observablecollection<t>(source); } messageboxes confirm via command, workers' employer properties being updated nothing i've tried makes textblock update. i've tried implementing raisepropertychanged on i've listed here no luck.
if set employment property of worker correct guild within constructor when data initialized, number in textblock displays correctly, doesn't update after that. hunch linq filtering , extension methods in workers property causing trouble, wrong.
if has ideas on how work, i'd love hear them. advice @ on matter appreciated. if need more code or information, please ask.
thanks.
update: think ron on right path; extension method may breaking binding. if case, can give me advice on how filter workers property in guild without breaking binding? also, far setter issue goes, added setter workers property never fires.
i think need redesign underlying data structure bit. may, change make work.
change workers property icollectionview so:
public icollectionview workers { get; set; }
then in guild model's contstructor can populate workers collection data manager so:
workers = collectionviewsource.getdefaultview(datamanager.data.workers);
and add filter icollectionview so:
workers.filter = (worker) => { return (worker.employer == this); };
and call workers.refresh() whenever workers collection updated.
that way binding won't break , workers collection retain same instance.
oh, , add updatesourcetrigger=propertychanged textbox binding.
like said, @ redesigning backing data structure entirely, without knowing why or how implemented cannot more that.
Comments
Post a Comment