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>

     最后,为LiraryStack 添加数据源。注意,不能将图片string[] 数组直接赋给LiraryStack,需要借助ObservableCollection。

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

运行界面:

LibraryStack

posted @ 2011-08-05 18:56  Gnie  阅读(4215)  评论(7编辑  收藏  举报
Copyright © 2010 Gnie