天天看點

WPF 與Surface 2.0 SDK 親密接觸–LibraryStack 篇

     LibraryStack 本身屬于ItemsControl,可以将其他元件以一種集合的方式顯示出來,使用者可以在LibraryStack 中逐一浏覽如圖檔類的元件,而且它預設支援拖拽操作。

     在下面的例子中我們将通過LibraryStack 展示一組圖檔。首先,為LibraryStack 編寫一個DataTemplate 用來綁定圖檔樣式。接下來在Grid 中添加LibraryStack 控件,并設定好資料模闆。

<s:SurfaceWindow x:Class="Demo.SurfaceWindow1"      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      xmlns:s="http://schemas.microsoft.com/surface/2008"      Title="LibraryStack"  >      <s:SurfaceWindow.Resources>          <DataTemplate x:Key="ItemTemplate">              <Image Source="{Binding}"/>          </DataTemplate>      </s:SurfaceWindow.Resources>        <Grid>          <s:LibraryStack x:Name="mLibraryStack" 
                        ItemTemplate="{StaticResource ItemTemplate}"/>      </Grid>  </s:SurfaceWindow>        
string imagesPath = @"C:\Users\Public\Pictures\Sample Pictures\";  try  {      string[] files = System.IO.Directory.GetFiles(imagesPath, "*.jpg");      ObservableCollection<string> items = new ObservableCollection<string>(files);      mLibraryStack.ItemsSource = items;  }  catch (System.IO.DirectoryNotFoundException)  {      // Error info.  }