c# - WPF: Binding a ItemsSource to a Directory -


i'm trying first wpf project, , i've started this sample project image display. part of xaml binds listbox array of images:

<listbox.itemssource>     <x:array type="{x:type imagesource}">         <imagesource>http://static.flickr.com/34/70703587_b35cf6d9eb.jpg</imagesource>         <imagesource>http://static.flickr.com/20/70703557_494d721b23.jpg</imagesource>         <imagesource>http://static.flickr.com/35/70703504_3ebf8f0150.jpg</imagesource>         <imagesource>http://static.flickr.com/35/70703474_18ef30450f.jpg</imagesource>     </x:array> </listbox.itemssource> 

now nice, bind images in subfolder , it's subfolders match pattern. directory structure this:

archive     1994-01         index.jpg         page1.jpg         ...         pagen.jpg     1994-02         index.jpg         page1.jpg         ...         pagen.jpg 

i want bind listbox various index.jpg images.

my normal approach codebehind using system.io , directory.getfiles, xaml seems rather powerful, i'm wondering: can type of binding achieved entirely in xaml?

as said, total beginner in wpf , want "proper" way, seems databinding.

the "proper" way wpf standpoint (separating code , presentation):

    public class indexreader: inotifypropertychanged     {         public ienumerable<string> indexfiles              { { ... } set { ... raise notify } }          public void readindeximagesfromfolder(string folder)         { ...         }     } 

you still use binding bind listbox (after set set instance of indexreader datacontext of listbox or 1 of parents):

<listbox itemssource="{binding indexfiles}"/> 

the rule is: if cannot bound easily, not attempt it.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -