FlipView Control in Windows 8

Introduction

 
In this article, we will describe the FlipView control in Windows 8. In Windows 8 we have to learn some new controls. Here, in this section we will display how to use FlipView control in various modes. By using the FlipView control we have to change the control orientation, bind the control to a collection, and create a context control to the current items.
 
So, we have to use the following steps to make this application.
 
Step 1
 
First of all you have to create a new Metro Style Application; let us see the description with images of how you will create it.
  • Open Visual Studio 2011
  • File -> New -> Project
  • Choose Template -> Visual C# -> Windows Metro Style -> Application
  • Rename this Application
homepage.gif
 
Step 2
 
In the Solution Explorer there are two files that we will primarily work with; MainPage.xaml and MainPage.xaml.cs files. In the images folder add any image to the application.
 
solutionexplorer.gif
 
Step 3
 
The MainPage.xaml file is as in the following code.
 
Code
 
Let us see the code which is given below. 
  1. <!-- Content -->  
  2.  <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="1" ZoomMode="Disabled">  
  3.         <StackPanel >  
  4.           <StackPanel x:Name="InputPanel" Orientation="Horizontal" HorizontalAlignment="Left">  
  5.             <StackPanel>  
  6.               <TextBlock Text="Input" Style="{StaticResource H2Style}"/>  
  7.               <TextBlock Text="Select Options" Style="{StaticResource H3Style}"/>  
  8.               <ListBox x:Name="ScenarioList" Margin="0,0,20,0" HorizontalAlignment="Left" Foreground="LightCoral">  
  9.                 <ListBox.ItemTemplate>  
  10.                  <DataTemplate>  
  11.                     <TextBlock Text="{Binding Name}"/>  
  12.                   </DataTemplate>  
  13.                 </ListBox.ItemTemplate>  
  14.                 <ListBoxItem x:Name="Scenario1">  
  15.                     <TextBlock Style="{StaticResource ListBoxTextStyle}" Text="1. Flipping through simple items" />  
  16.                 </ListBoxItem>  
  17.                 <ListBoxItem x:Name="Scenario2">  
  18.                     <TextBlock Style="{StaticResource ListBoxTextStyle}" Text="2. FlipView orientation" />  
  19.                 </ListBoxItem>  
  20.                 <ListBoxItem x:Name="Scenario3">  
  21.                     <TextBlock Style="{StaticResource ListBoxTextStyle}" Text="3. Binding to a collection" />  
  22.                 </ListBoxItem>  
  23.                 <ListBoxItem x:Name="Scenario4">  
  24.                     <TextBlock Style="{StaticResource ListBoxTextStyle}" Text="4. Nested FlipViews" />  
  25.                 </ListBoxItem>  
  26.                 <ListBoxItem x:Name="Scenario5">  
  27.                     <TextBlock Style="{StaticResource ListBoxTextStyle}" Text="5. FlipView context control" />  
  28.                 </ListBoxItem>  
  29.             </ListBox>  
  30.             </StackPanel>  
  31.             <StackPanel Margin="0,31,0,0" >  
  32.               <TextBlock Text="Description:" Style="{StaticResource H3Style}"/>  
  33.              <StackPanel x:Name="Description" MaxWidth="800">                    
  34.                 <!-- Scenario 1 -->  
  35.                 <StackPanel x:Name="Scenario1Input">  
  36.                     <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" >  
  37.                         This section explains populating the FlipView control with a series of simple items via XAML.  
  38.                     </TextBlock>  
  39.                     <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Margin="0,20,0,0" />  
  40.                     <StackPanel Orientation="Vertical" Margin="0,10,0,0">  
  41.                     </StackPanel>  
  42.                 </StackPanel>                    
  43.                 <!-- Scenario 2 -->  
  44.                 <StackPanel x:Name="Scenario2Input" Visibility="Collapsed">  
  45.                     <StackPanel Orientation="Vertical" Margin="0,10,0,0">  
  46.                         <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" >  
  47.                             This section describes how to orient the FlipView vertically and horizontally.  Use the FlipView to move  
  48.                             through the items in one orientation, then click the button below to switch orientations and interact with the  
  49.                             FlipView again to move through the items in the opposite direction.  
  50.                         </TextBlock>  
  51.                         <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Margin="0,20,0,0" />  
  52.                         <Button x:Name="OrientationButton" Content="Vertical" Click="OrientationButton_Click"/>  
  53.                     </StackPanel>  
  54.                 </StackPanel>                    
  55.                 <!-- Scenario 3 -->  
  56.                 <StackPanel x:Name="Scenario3Input" Visibility="Collapsed">  
  57.                     <StackPanel Orientation="Vertical" Margin="0,10,0,0">  
  58.                         <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" >  
  59.                             This section describes how to bind the FlipView to a collection of items.  
  60.                             The FlipView is bound to a collection of strings that represent image files.  
  61.                         </TextBlock>  
  62.                         <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Margin="0,20,0,0" />  
  63.                     </StackPanel>  
  64.                 </StackPanel>  
  65.                 <!-- Scenario 4 -->  
  66.                 <StackPanel x:Name="Scenario4Input">  
  67.                     <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" >  
  68.                         This scenario demonstrates nesting two FlipView controls orthogonally to provide the capability  
  69.                         of navigating multiple items that have multiple items (horizontal and vertical scrolling).  
  70.                     </TextBlock>  
  71.                     <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Margin="0,20,0,0" />  
  72.                 </StackPanel>  
  73.                   <!-- Scenario 5 -->  
  74.                 <StackPanel x:Name="Scenario5Input">  
  75.                     <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" >  
  76.                         This section describes how to create a FlipView "context control" to manage a small number of  
  77.                         items within a FlipView.  Navigate within the FlipView and notice that the context control will  
  78.                         provide a visual indication where you are with respect to the entire collection.  You can also click  
  79.                         on one of the items in the context control to navigate directly to one of the items in the collection.  
  80.                     </TextBlock>  
  81.                     <TextBlock Style="{StaticResource DescriptionTextStyle}" TextWrapping="Wrap" Margin="0,20,0,0" />  
  82.                 </StackPanel>  
  83.             </StackPanel>  
  84.             </StackPanel>  
  85.           </StackPanel>              
  86.             <!-- Output section -->  
  87.           <TextBlock Text="Output" Margin="0,25,0,20" Style="{StaticResource H2Style}"/>  
  88.           <StackPanel x:Name="Output"  HorizontalAlignment="Left">                
  89.               <!-- Item 1-->  
  90.             <StackPanel x:Name="Scenario1Output">  
  91.             <!-- Add simple items to the FlipView by adding them inline as children of the FlipView. -->  
  92.             <FlipView x:Name="FlipView1" Width="300" Height="300">  
  93.                 <FlipViewItem Width="298" Height="298">  
  94.                     <Border Background="Orange">  
  95.                         <TextBlock Text="Item 1" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  96.                     </Border>  
  97.                 </FlipViewItem>  
  98.                 <FlipViewItem Width="298" Height="298">  
  99.                     <Border Background="LightBlue">  
  100.                         <TextBlock Text="Item 2" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  101.                    </Border>  
  102.                 </FlipViewItem>  
  103.                 <FlipViewItem Width="298" Height="298">  
  104.                     <Image Source="Images/image1.jpg" Stretch="UniformToFill"/>  
  105.                 </FlipViewItem>  
  106.                 <FlipViewItem Width="298" Height="298">  
  107.                     <Border Background="LightGreen">  
  108.                         <TextBlock Text="Item 4" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  109.                     </Border>  
  110.                 </FlipViewItem>  
  111.             </FlipView>  
  112.         </StackPanel>  
  113.              <!-- Item 2 -->  
  114.             <StackPanel x:Name="Scenario2Output" >  
  115.              <FlipView x:Name="FlipView2Horizontal" Width="300" Height="300" Visibility="Visible">  
  116.                 <FlipView.ItemsPanel>  
  117.                     <ItemsPanelTemplate>  
  118.                         <StackPanel x:Name="FlipView2ItemsPanel" Background="Transparent" Orientation="Horizontal"/>  
  119.                     </ItemsPanelTemplate>  
  120.                 </FlipView.ItemsPanel>  
  121.                 <FlipViewItem Width="298" Height="298">  
  122.                     <Border Background="Orange">  
  123.                         <TextBlock Text="Item 1" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  124.                     </Border>  
  125.                 </FlipViewItem>  
  126.                 <FlipViewItem Width="298" Height="298">  
  127.                     <Border Background="LightBlue">  
  128.                         <TextBlock Text="Item 2" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  129.                     </Border>  
  130.                 </FlipViewItem>  
  131.                 <FlipViewItem Width="298" Height="298">  
  132.                     <Border Background="LightGray">  
  133.                         <TextBlock Text="Item 3" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  134.                     </Border>  
  135.                 </FlipViewItem>  
  136.             </FlipView>  
  137.            <!-- The Vertical FlipView -->  
  138.             <FlipView x:Name="FlipView2Vertical" Width="300" Height="300" Visibility="Collapsed">  
  139.                 <FlipView.ItemsPanel>  
  140.                     <ItemsPanelTemplate>  
  141.                         <StackPanel x:Name="FlipView2ItemsPanel" Background="Transparent" Orientation="Vertical"/>  
  142.                     </ItemsPanelTemplate>  
  143.                 </FlipView.ItemsPanel>  
  144.                 <FlipViewItem Width="298" Height="298">  
  145.                     <Border Background="Orange">  
  146.                         <TextBlock Text="Item 1" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  147.                     </Border>  
  148.                 </FlipViewItem>  
  149.                 <FlipViewItem Width="298" Height="298">  
  150.                     <Border Background="LightCoral">  
  151.                         <TextBlock Text="Item 2" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  152.                     </Border>  
  153.                 </FlipViewItem>  
  154.                 <FlipViewItem Width="298" Height="298">  
  155.                     <Border Background="Green">  
  156.                         <TextBlock Text="Item 3" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  157.                     </Border>  
  158.                 </FlipViewItem>  
  159.             </FlipView>  
  160.         </StackPanel>  
  161.                  <!-- Scenario 3 -->  
  162.             <StackPanel x:Name="Scenario3Output" >  
  163.             <!-- This scenario illustrates a simple data binding example where we bind a collection of strings that represent image paths.  -->  
  164.                 <StackPanel Orientation="Vertical">  
  165.                     <FlipView x:Name="FlipView3" Width="300" Height="300">  
  166.                         <FlipView.ItemTemplate>  
  167.                             <DataTemplate>  
  168.                                 <Image Source="{Binding}" Stretch="UniformToFill" Height="300" Width="300"/>  
  169.                             </DataTemplate>  
  170.                         </FlipView.ItemTemplate>  
  171.                     </FlipView>  
  172.                 </StackPanel>  
  173.             </StackPanel>  
  174.             <StackPanel x:Name="Scenario4Output">  
  175.                     <!-- Horizontal FlipView -->  
  176.                 <FlipView x:Name="OuterFlipView" Width="300" Height="300">  
  177.                     <FlipView.ItemsPanel>  
  178.                         <ItemsPanelTemplate>  
  179.                             <StackPanel x:Name="OuterItemsPanel" Background="Transparent" Orientation="Horizontal"/>  
  180.                         </ItemsPanelTemplate>  
  181.                     </FlipView.ItemsPanel>  
  182.                     <FlipViewItem Width="298" Height="298">  
  183.                         <Border Background="Orange">  
  184.                  <TextBlock Text="FlipView Item1" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  185.                         </Border>  
  186.                     </FlipViewItem>  
  187.                     <FlipViewItem Width="298" Height="298">  
  188.                         <Border Background="Blue">  
  189.                  <TextBlock Text="FlipView Item2" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  190.                         </Border>  
  191.                     </FlipViewItem>  
  192.                     <FlipViewItem Width="298" Height="298">  
  193.                         <Border Background="Green">  
  194.                  <TextBlock Text="FlipView Item3" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  195.                         </Border>  
  196.                     </FlipViewItem>  
  197.                     <FlipViewItem Width="298" Height="298">  
  198.                         <Border Background="Pink">  
  199.                  <TextBlock Text="FlipView Item4" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  200.                         </Border>  
  201.                     </FlipViewItem>  
  202.                     <FlipViewItem Width="298" Height="298">  
  203.                         <!-- Nested Vertical FlipView.  Notice that this FlipView is actually a FlipViewItem which acheives the nestedeffect.  -->  
  204.                         <FlipView x:Name="InnerFlipView" Width="298" Height="298">  
  205.                             <FlipView.ItemsPanel>  
  206.                                 <ItemsPanelTemplate>  
  207.                                     <StackPanel x:Name="InnerItemsPanel" Background="Transparent" Orientation="Vertical"/>  
  208.                                 </ItemsPanelTemplate>  
  209.                             </FlipView.ItemsPanel>  
  210.                             <FlipViewItem Width="298" Height="298">  
  211.                                 <Border Background="Orange">  
  212.        <TextBlock Text="Vertical FlipView Item1" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  213.                                 </Border>  
  214.                             </FlipViewItem>  
  215.                             <FlipViewItem Width="298" Height="298">  
  216.                                 <Border Background="Lightcyan">  
  217.              <TextBlock Text="FlipView Item2" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  218.                                 </Border>  
  219.                             </FlipViewItem>  
  220.                             <FlipViewItem Width="298" Height="298">  
  221.                                 <Border Background="DarkOrange">  
  222.      <TextBlock Text="Vertical FlipView Item3" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  223.                                 </Border>  
  224.                             </FlipViewItem>  
  225.                             <FlipViewItem Width="298" Height="298">  
  226.                                 <Border Background="Pink">  
  227.                <TextBlock Text="FlipView Item4" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource DescriptionTextStyle}"/>  
  228.                                 </Border>  
  229.                             </FlipViewItem>  
  230.                         </FlipView>  
  231.                     </FlipViewItem>  
  232.                 </FlipView>  
  233.             </StackPanel>  
  234.             <StackPanel x:Name="Scenario5Output">  
  235.                    <StackPanel Orientation="Vertical" HorizontalAlignment="Center">  
  236.                     <FlipView x:Name="FlipView5" Height="300" Width="300">  
  237.                         <FlipView.ItemTemplate>  
  238.                             <DataTemplate>  
  239.                                 <Image Source="{Binding}" Stretch="UniformToFill" Height="300" Width="300"/>  
  240.                             </DataTemplate>  
  241.                         </FlipView.ItemTemplate>  
  242.                     </FlipView>  
  243.                     <ListBox x:Name="ContextControl"  
  244. SelectedItem="{Binding SelectedItem, ElementName=FlipView5, Mode=TwoWay}"  
  245. ItemContainerStyle="{StaticResource RadioButtonListBoxItemStyle}"  
  246. HorizontalAlignment="Center" Margin="0,5,0,0"IsTabStop="False">  
  247.                         <ListBox.ItemsPanel>  
  248.                             <ItemsPanelTemplate>  
  249.                                 <StackPanel Orientation="Horizontal"/>  
  250.                             </ItemsPanelTemplate>  
  251.                         </ListBox.ItemsPanel>  
  252.                     </ListBox>  
  253.                 </StackPanel>  
  254.             </StackPanel>  
  255.         </StackPanel>  
  256.         </StackPanel>  
  257.       </ScrollViewer>  
  258.          </Grid>  
  259.   </Grid>  
  260. </UserControl>  
Step 4
 
After running this code we get the following output.
 
output1.gif
 
output2.gif
 
output3.gif
 
output4.gif
 
output5.gif
 

Summary

 
In this article, we learned about FlipView Control in Windows 8. 
 
Resources
 
Here are some useful resources.


Similar Articles