i trying bind listbox via itemstemplate collection of custom "document" objects having issue while trying bind image document.imageresourcepath property. here markup
<listbox name="lbdocuments"> <listbox.itemtemplate> <datatemplate> <stackpanel> <image source="{binding path=imageresourcepath}" margin="5,0,5,0"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> this load event form has listbox.
private void window_loaded_1(object sender, routedeventargs e) { list<objects.document> docs = objects.document.fetchdocuments(); lbdocuments.itemssource = docs; } my document class holds string resource image located in resources folder depending on document extension.
e.g. (this part of case statement within document class)
case cache.documenttype.pdf: this.imageresourcepath = "/juvenileorganizationemail;component/resources/pdf_icon.jpg"; break; when window loads absolutely nothing in listbox when bound 23 document types. doing wrong?
use observablecollection instead of list, , make reference "class level" window.
observablecollection<objects.document> _docs; make sure datacontext set in window's ctor.
public window() { _docs = new observablecollection<objects.document>(objects.document.fetchdocuments()); this.datacontext = this; } then, can update window loaded event:
private void window_loaded_1(object sender, routedeventargs e) { lbdocuments.itemssource = _docs; } or, alternative solution, binding itemssource of listbox directly public property of collection. assuming ctor (above) still used.
<listbox name="lbdocuments" itemssource={binding docs}> <listbox.itemtemplate> <datatemplate> <stackpanel> <image source="{binding path=imageresourcepath}" margin="5,0,5,0"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> in window.cpp file (though, separate viewmodel class may recommended if doing mvvm)
public observablecollection<objects.document> docs { { return _docs; } }
Comments
Post a Comment