Rumah > Artikel > pangkalan data > WPF 与Surface 2.0 SDK 亲密接触–LibraryStack 篇
LibraryStack 本身属于ItemsControl,可以将其他组件以一种集合的方式显示出来,用户可以在LibraryStack 中逐一浏览如图片类的组件,而且它默认支持拖拽操作。 在下面的例子中我们将通过LibraryStack 展示一组图片。首先,为LibraryStack 编写一个DataTemplat
LibraryStack 本身属于ItemsControl,可以将其他组件以一种集合的方式显示出来,用户可以在LibraryStack 中逐一浏览如图片类的组件,而且它默认支持拖拽操作。在下面的例子中我们将通过LibraryStack 展示一组图片。首先,为LibraryStack 编写一个DataTemplate 用来绑定图片样式。接下来在Grid 中添加LibraryStack 控件,并设置好数据模板。
<span><span>s</span><span>:</span><span>SurfaceWindow </span><span>x</span><span>:</span><span>Class</span><span>="Demo.SurfaceWindow1" </span><span>xmlns</span><span>="http://schemas.microsoft.com/winfx/2006/xaml/presentation" </span><span>xmlns</span><span>:</span><span>x</span><span>="http://schemas.microsoft.com/winfx/2006/xaml" </span><span>xmlns</span><span>:</span><span>s</span><span>="http://schemas.microsoft.com/surface/2008" </span><span>Title</span><span>="LibraryStack" > <span>s</span><span>:</span><span>SurfaceWindow.Resources</span><span>> <span>DataTemplate </span><span>x</span><span>:</span><span>Key</span><span>="ItemTemplate"> <span>Image </span><span>Source</span><span>="{</span><span>Binding</span><span>}"/> </span><span>DataTemplate</span><span>> </span><span>s</span><span>:</span><span>SurfaceWindow.Resources</span><span>> <span>Grid</span><span>> <span>s</span><span>:</span><span>LibraryStack </span><span>x:Name</span><span>="mLibraryStack" </span><span>ItemTemplate</span><span>="{</span><span>StaticResource </span><span>ItemTemplate</span><span>}"/> </span><span>Grid</span><span>> </span><span>s</span><span>:</span><span>SurfaceWindow</span><span>> </span></span></span></span></span></span></span>
最后,为LiraryStack 添加数据源。注意,不能将图片string[] 数组直接赋给LiraryStack,需要借助ObservableCollection。
<span>string </span>imagesPath = <span>@"C:\Users\Public\Pictures\Sample Pictures\"</span>; <span>try </span>{ <span>string</span>[] files = System.IO.<span>Directory</span>.GetFiles(imagesPath, <span>"*.jpg"</span>); <span>ObservableCollection</span>string> items = <span>new </span><span>ObservableCollection</span>string>(files); mLibraryStack.ItemsSource = items; } <span>catch </span>(System.IO.<span>DirectoryNotFoundException</span>) { <span>// Error info. </span>}
运行界面:
作者:李敬然(Gnie)
出处:{GnieTech} (http://www.cnblogs.com/gnielee/)