c# - How to bind a collection to a ListView in WPF -


i have program searches directory files matching criteria. search process takes long time, have call asynchronously. when search algorithm finds file, triggers event. mainwindow instance listens event , needs update gui. how can bind these "added" files listview? figured use observablecollection<fileinfo> instance, can't figure out how bind it.

i've stripped out of irrelevant controls , code. here 2 relevant files.

mainwindow.xaml:

<window x:class="example.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="cr search" height="395" width="525">     <grid>         <listview x:name="results">             <listview.view>                 <gridview>                     <gridviewcolumn header="filename"/>                     <gridviewcolumn header="directory"/>                 </gridview>             </listview.view>         </listview>     </grid> </window> 

mainwindow.xaml.cs:

using system.io; using system.threading.tasks;  public partial class mainwindow {     private searchlogic _backgroundsearch;      private async void search(object sender, routedeventargs e)     {         // todo: clear results          _backgroundsearch = new searchlogic("", new directoryinfo("c:\"));         _backgroundsearch.fileadded += fileadded;          await task.run(new action(_backgroundsearch.search));     }      private void fileadded(object sender, fileaddedeventargs eventargs)     {         // todo: add eventargs.file results         // eventargs.file instance of fileinfo     } } 

here simple example

your xaml

<window x:class="wpfapplication10.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow"         width="525"         height="350"         loaded="window_loaded">     <grid>         <listbox itemssource="{binding filenames}">             <listbox.itemtemplate>                 <datatemplate>                     <stackpanel orientation="vertical">                         <label>name</label>                         <textblock text="{binding name}"/>                         <label>modified</label>                         <textblock text="{binding lastmodified}"/>                                             </stackpanel>                 </datatemplate>             </listbox.itemtemplate>         </listbox>     </grid> </window> 

your code behind

public partial class mainwindow : window {     public class fileinfo     {         public string name { get; set; }         public datetime lastmodified { get; set; }         public fileinfo(string name)         {             name = name;             lastmodified = datetime.now;         }     }      observablecollection<fileinfo> mfilenames = new observablecollection<fileinfo>();      public observablecollection<fileinfo> filenames     {                 {             return mfilenames;         }     }      public mainwindow()     {         datacontext = this;         initializecomponent();     }      private void window_loaded(object sender, routedeventargs e)     {         threadpool.queueuserworkitem((x) =>             {                 while (true)                 {                     dispatcher.begininvoke((action)(() =>                     {                         mfilenames.add(new fileinfo("x"));                     }));                     thread.sleep(500);                 }             });     } } 

if run problem notice listbox updates every half second new item. key thing note observablecollection can updated ui thread if refactor above code need need somehow use dispatcher of current ui thread update it


Comments

Popular posts from this blog

stringtemplate - StringTemplate4 if conditional with length -

shader - OpenGL Shadow Map -