2010-01-15 85 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}"/> 

規則是:如果不能很容易地綁定,請不要嘗試它。

相關問題