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