c# - WPF ListBox bind ItemsSource with MVVM-light -
xaml
<window x:class="html5mapper.mapper.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpt="http://schemas.xceed.com/wpf/xaml/toolkit" title="html mapper" height="300" width="600" > <window.datacontext> <binding path="main" source="{staticresource locator}"/> </window.datacontext> <listbox name="lbfiles" itemssource="{binding filescollection, mode=oneway}" width="240" height="220"> <listbox.itemtemplate> <datatemplate> <grid margin="0,2"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="100" /> </grid.columndefinitions> <image source="html.png" height="40" width="32" /> <textblock grid.column="1" text="{binding name}" /> </grid> </datatemplate> </listbox.itemtemplate> </listbox>
mainviewmodel.cs
public observablecollection<files> filescollection { get; set; } public mainviewmodel() { this.filescollection = new observablecollection<files>(); selectfilesaction(); } private void selectfilesaction() { this.filescollection.add(new files { name = "index.html", path = "c:\\files"}); //lbfiles.itemssource = docs; }
q: else need docs object listbox ?
in opinion binding controls wrong datacontect, check output window erros. might want set datacontext of main window mainviewmodel (in codebehind or similar). why create instance docs? useless.
public observablecollection<files> filescollection {get;set;} public mainviewmodel() { this.filescollection = new observablecollection<files>(); selectfilesaction(); } private void selectfilesaction() { filescollection.add(new files { name = "index.html", path = "c:\\files"}); }
Comments
Post a Comment