2010-01-15 107 views
1

我正在尝试执行我的第一个WPF项目,并且我已经开始使用this sample project进行图像显示。部分原因是结合一个列表框图像阵列的XAML:WPF:将ItemsSource绑定到目录

<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> 

现在是不错,但我想将其绑定到所有图像的子文件夹,它是模式匹配的子文件夹。我的目录结构如下:

Archive 
    1994-01 
     Index.jpg 
     Page1.jpg 
     ... 
     PageN.jpg 
    1994-02 
     Index.jpg 
     Page1.jpg 
     ... 
     PageN.jpg 

我想将Listbox绑定到各种Index.jpg图像上。

我通常的做法是使用System.IO和Directory.GetFiles来做一些CodeBehind,但由于XAML看起来相当强大,我只是想知道:这种绑定方式是否可以完全在XAML中实现?

如前所述,WPF中的初学者和我想要做到“正确”的方式,这似乎是DataBinding。

回答

4

从WPF角度来看,“正确”的方式会是这样(分离代码和演示):

public class IndexReader: INotifyPropertyChanged 
    { 
     public IEnumerable<string> IndexFiles 
      { get { ... } set { ... raise notify } } 

     public void ReadIndexImagesFromFolder(string folder) 
     { 
... 
     } 
    } 

你仍然会使用绑定来绑定到列表框(您设置的IndexReader的一组实例后ListBox的DataContext或其父项之一):

<ListBox ItemsSource="{Binding IndexFiles}"/> 

规则是:如果不能很容易地绑定,请不要尝试它。

相关问题