Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a list of critter images with their names in XAML
DescriptionThis example shows how to make a list of critter images with their names in XAML. It demonstrates nested StackPanels, page resources, and linear gradient brushes.
KeywordsXAML, WPF, loose xaml, critters, images, WPF style, page, Page.Resources, page resources, linear gradient brush, LinearGradientBrush
CategoriesVB.NET, WPF, Internet
 

This example demonstrates several useful XAML techniques.

First, this is a loose XAML page. It uses a Page element as its root rather than a Window because the Window class requires extra permissions on the client computer.

Attributes on the Page set the Page's FontFamily and FontSize. These are inherited by controls contained in the Page unless they are overridden in those controls.

The Page's Resources section stores resourcs for the Page. In this example, that includes a Style for Image controls and a Style for Label controls. The Styles use Setters to define properties that should be set for those controls.

Note that some properties cannot be set in a loose XAML page. For example, BitmapEffects are implemented in unmanaged code, which requires extra permissions so is not allowed in a loose XAML page.

Note also that the Page would not accept a FontWeight attribute, although the Label control's Style did so I set it in that Style.

The Page's Background property defines the Page's background brush. In this case, it is a linear gradient brush shading from light blue at the top to dark blue at the bottom.

Finally the Page contains a StackPanel oriented horizontally. That contains a series of StackPanels oriented vertically, each containing an Image and a Label. Note that the JPEG files must be in the same location as the XAML file.

The Image and Label controls set their Style properties to the Styles previously created in the Page's resources. Those Styles set the controls' HorizontalAlignment properties to Center so the images and text are centered in their entries in their StackPanels.

 
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    FontFamily="Comic Sans MS" FontSize="20"
>
    <Page.Resources>
        <Style x:Key="ImageStyle" TargetType="{x:Type Image}">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Height" Value="50"/>
        </Style>
        <Style x:Key="LabelStyle" TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Margin" Value="5"/>
        </Style>
    </Page.Resources>
    <Page.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF80FFFF" Offset="0"/>
            <GradientStop Color="#FF042CFF" Offset="1"/>
        </LinearGradientBrush>
    </Page.Background>
    <StackPanel Orientation="Horizontal" Margin="5">
        <StackPanel>
            <Image Source="Frog.jpg" Style="{StaticResource ImageStyle}"/>
            <Label Content="Frog" Style="{StaticResource LabelStyle}"/>
        </StackPanel>
        <StackPanel>
            <Image Source="Butterfly.jpg" Style="{StaticResource ImageStyle}"/>
            <Label Content="Butterfly" Style="{StaticResource LabelStyle}"/>
        </StackPanel>
        <StackPanel>
            <Image Source="Shark.jpg" Style="{StaticResource ImageStyle}"/>
            <Label Content="Shark" Style="{StaticResource LabelStyle}"/>
        </StackPanel>
        <StackPanel>
            <Image Source="Tiger.jpg" Style="{StaticResource ImageStyle}"/>
            <Label Content="Tiger" Style="{StaticResource LabelStyle}"/>
        </StackPanel>
        <StackPanel>
            <Image Source="Platypus.jpg" Style="{StaticResource ImageStyle}"/>
            <Label Content="Platypus" Style="{StaticResource LabelStyle}"/>
        </StackPanel>
    </StackPanel>
</Page>
 
IMPORTANT: This XAML page should display in a XAML-enabled browser such as Internet Explorer or Firefox, assuming you have the .NET Framework version 3.5 or later installed. I was able to get it to run locally by double-clicking the file or by browsing to it with Internet Explorer or Firefox. However, when I move the file to my Web site neither browser can open it. Internet Explorer gives a typically uninformative error message and Firefox displays the file as text.

If you know what the problem is, please let me know!

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated