WPF Listbox Items -
i have listbox control contains names of files inside directory.
how iterate though controls , names? i've tried:
for (int = 0; < listboxfilegroups.items.count; i++) { // don't want use properties start selected // here looking string textitem = listboxfilegroups.items[i].tostring(); }
any suggestions?
you may want explore mvvm approach.
in 1 of view model classes, can have observablecollection<string>
:
public observablecollection<string> stuffforlistbox { get; set; }
populate observablecollection want displayed in listbox. in code-behind of usercontrol or window in have listbox, set datacontext instance of class containing stuffforlistbox
seen above.
this.datacontext = new myclass();
alternatively create datatemplate usercontrol / window automagically wire datacontext view model.
since mentioned want display files in directory (not including sub-directories), need bind itemssource stuffforlistbox.
<listbox itemssource="{binding stuffforlistbox}" ... >
to iterate through strings displayed in listbox need iterate through observablecollection.
if don't want bother mvvm or if third party listbox, can try grabbing itemssource in codebehind , loop through i'd recommend mvvm. it'll make life easier.
now, if wanted little crazier , display things subfolders observablecollection<string>
won't cut it. need create class contains children model how folder has files , subfolders.
public class demoitem { public string name { get; set; } public demoitem parent { get; set; } public observablecollection<demoitem> children { get; set; } public bool isselected { get; set; } }
...and base observable collection thats bound listbox on above class.
if , when that, listbox won't display items until create datatemplate suppose that't outside of scope of question :p
Comments
Post a Comment