Export (0) Print
Expand All

Using the LibraryStack Control

The LibraryStack control that is included in the Microsoft Surface Toolkit for Windows Touch Beta is an ItemsControl object that enables you to display items that are stacked on each other. Users can view the individual items by rearranging the order of the stack or by removing items from the stack. By default, the LibraryStack control supports drag-and-drop operations. See Using Drag-and-Drop with the LibraryBar and LibraryStack Controls for more information.

LibraryStack - Close-up example

In the following code example, a LibraryStack control is created as part of a SurfaceWindow object. The DataTemplate for the LibraryStack control is set to show the images that are bound to the control.


<s:SurfaceWindow 
    x:Class="LibraryStack.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">

    <Grid>
        <s:LibraryStack Name="MainLibraryStack">
            <s:LibraryStack.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding}"/>
                </DataTemplate>
            </s:LibraryStack.ItemTemplate>
        </s:LibraryStack>
    </Grid>
    
</s:SurfaceWindow>

In the code for SurfaceWindow, the OnInitialized method is overridden to set the ItemsSource property of the LibraryStack control to a collection of sample images.


protected override void OnInitialized(EventArgs e)
{
    base.OnInitialized(e);

    // Query the registry to find out where the sample photos are stored.
    const string shellKey =
        @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\Shell Folders";

    string imagesPath =
        (string)Microsoft.Win32.Registry.GetValue(shellKey, "CommonPictures", null) + @"\Sample Pictures";

    try
    {
        // Get the list of files.
        string[] files = System.IO.Directory.GetFiles(imagesPath, "*.jpg");

        // Create an ObservableCollection from the file names.
        // Cannot assign string[] files to LibraryStack.ItemsSource.
        // LibraryStack.ItemsSource must implement INotifyCollectionChanged.
        ObservableCollection<string> items = new ObservableCollection<string>(files);

        // Set the ItemsSource property.
        MainLibraryStack.ItemsSource = items;
    }
    catch (System.IO.DirectoryNotFoundException)
    {
        // Handle exception as needed.
    }
}

See Also

Community Additions

Show:
© 2014 Microsoft