list - WPF MVVM: View's ListBox with source deep in Model. How to implement? -



list - WPF MVVM: View's ListBox with source deep in Model. How to implement? -

i'm new wpf. need bind ui's listbox source deep in model layer.

app scheme on image below. desc:

my mainwindowviewmodel class has scheduler property (scheduler class in model layer).

scheduler class has currentparser property (parser class in model layer).

parser class has result field (parserresultmetadata class in model layer).

parserresultmetadata class has log field (log list(of string))

log can changed programmatically model layer (parser adds lines during it's work).

so question how can bind listbox list match mvvm pattern. now, viewmodel must have observablecollection(of string) witch re-create of list(of string) model layer.

somehow need notify ui when line added collection. there multiple ways accomplish this, if collection modified within model layer, need mechanism communicating other layers in 1 way or another.

use observablecollection in model layer.

while types observablecollection , inotifypropertychanged used in mvvm architectures, not specific them , in cases can create sense utilize them in model layer directly. using observablecollection in parser class 1 way provide notification mechanism. can bind itemssource of listbox scheduler.parser.result.log straight , update accordingly.

create wrapper property in viewmodel.

if don't want utilize observablecollection in model, can expose list via property in viewmodel, example:

public ienumerable<string> parserlog { { homecoming scheduler.parser.result.log; } }

then need manually notify ui when item added, you're gonna need event (or equivalent) tells viewmodel list changed , needs raise propertychanged event parserlog property. add together code in viewmodel:

this.scheduler.parser.resultupdated += (s, e) => this.raisepropertychanged("parserlog");

this tell listbox update items parserlog property.

wpf list mvvm viewmodel observablecollection

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -