windows presentation foundation tutorial 1 - …billatnapier.com/wpf.pdf · w.buchanan 1 windows...

12
W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF: http://billdotnet.com/wpf.pdf Tutorial: http://billdotnet.com/dotnet_lecture/dotnet_lecture.htm A Introduction 1. Start Visual Studio 2008, and select a WPF Application. Next add the following highlighted XAML code: <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="304" Width="466"> <Grid> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Button>First button< <Button>First button< <Button>First button< <Button>First button</Button> Button> Button> Button> </Border> </Border> </Border> </Border> </Grid> </Window> 2. Next try the following colours for the Border: AliceBlue, CornflowerBlue and MediumBlue. 3. Next try to change the colour of the text on the button to AliceBlue. 4. Change the name of the form to “Napier”. B Exporting and importing XAML 5. Install Microsoft Expression Design (Figure 1), and start the package, and create the Scottish flag (see below). Once complete export it to c:\scotland.xaml. Figure 1: Microsoft Expression Design

Upload: trinhtruc

Post on 06-May-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 1

Windows Presentation Foundation Tutorial 1 PDF: http://billdotnet.com/wpf.pdf

Tutorial: http://billdotnet.com/dotnet_lecture/dotnet_lecture.htm

A Introduction 1. Start Visual Studio 2008, and select a WPF Application. Next add the

following highlighted XAML code:

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="304" Width="466">

<Grid>

<Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"> <Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5">

<Button>First button< <Button>First button< <Button>First button< <Button>First button<////Button>Button>Button>Button>

</Border> </Border> </Border> </Border>

</Grid>

</Window>

2. Next try the following colours for the Border: AliceBlue, CornflowerBlue and

MediumBlue.

3. Next try to change the colour of the text on the button to AliceBlue.

4. Change the name of the form to “Napier”.

B Exporting and importing XAML 5. Install Microsoft Expression Design (Figure 1), and start the package, and

create the Scottish flag (see below). Once complete export it to

c:\scotland.xaml.

Figure 1: Microsoft Expression Design

Page 2: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 2

Note: if you cannot install with package on your computer, you can download

the exported XML from:

http://billdotnet.com/scotland.zip

6. Next import the XAML into the form (such as in the highlighted text). Figure

2 shows the result of a sample run.

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Napier" Height="304" Width="466">

<Grid>

<Button> <Button> <Button> <Button>

<Canvas xmlns="h <Canvas xmlns="h <Canvas xmlns="h <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ttp://schemas.microsoft.com/winfx/2006/xaml/presentation" ttp://schemas.microsoft.com/winfx/2006/xaml/presentation" ttp://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:c="http://schemas.openxmlformats.org/markupxmlns:c="http://schemas.openxmlformats.org/markupxmlns:c="http://schemas.openxmlformats.org/markupxmlns:c="http://schemas.openxmlformats.org/markup----compatibility/2006" compatibility/2006" compatibility/2006" compatibility/2006"

xmlns:d="http://schemas.microsoft.com/expression/blend/2006" c:Ignorable=xmlns:d="http://schemas.microsoft.com/expression/blend/2006" c:Ignorable=xmlns:d="http://schemas.microsoft.com/expression/blend/2006" c:Ignorable=xmlns:d="http://schemas.microsoft.com/expression/blend/2006" c:Ignorable="d" "d" "d" "d"

x:Name="Untitled1" Width="800" Height="600">x:Name="Untitled1" Width="800" Height="600">x:Name="Untitled1" Width="800" Height="600">x:Name="Untitled1" Width="800" Height="600">

<Canvas x:Name="Layer_1" d:IsLayer="True" Width="800" Height="600" <Canvas x:Name="Layer_1" d:IsLayer="True" Width="800" Height="600" <Canvas x:Name="Layer_1" d:IsLayer="True" Width="800" Height="600" <Canvas x:Name="Layer_1" d:IsLayer="True" Width="800" Height="600"

Canvas.Left="0" Canvas.Top="0">Canvas.Left="0" Canvas.Top="0">Canvas.Left="0" Canvas.Top="0">Canvas.Left="0" Canvas.Top="0">

<Rectangle x:Name="Rectangle" Width="288" Height="197" Canvas.Left="74" <Rectangle x:Name="Rectangle" Width="288" Height="197" Canvas.Left="74" <Rectangle x:Name="Rectangle" Width="288" Height="197" Canvas.Left="74" <Rectangle x:Name="Rectangle" Width="288" Height="197" Canvas.Left="74"

Canvas.Top="27" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Canvas.Top="27" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Canvas.Top="27" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000" Canvas.Top="27" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FF000000"

Fill="#FF230FD2"/>Fill="#FF230FD2"/>Fill="#FF230FD2"/>Fill="#FF230FD2"/>

<Path x:Name="Line" Width="309" Height="216" Canvas.Left="64" <Path x:Name="Line" Width="309" Height="216" Canvas.Left="64" <Path x:Name="Line" Width="309" Height="216" Canvas.Left="64" <Path x:Name="Line" Width="309" Height="216" Canvas.Left="64"

Canvas.Top="17" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Canvas.Top="17" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Canvas.Top="17" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Canvas.Top="17" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round"

Stroke=Stroke=Stroke=Stroke="#FFFFFFFF" Data="F1 M 70,116L 391.333,333.333"/>"#FFFFFFFF" Data="F1 M 70,116L 391.333,333.333"/>"#FFFFFFFF" Data="F1 M 70,116L 391.333,333.333"/>"#FFFFFFFF" Data="F1 M 70,116L 391.333,333.333"/>

<Path x:Name="Line_0" Width="288" Height="197" Canvas.Left="74" <Path x:Name="Line_0" Width="288" Height="197" Canvas.Left="74" <Path x:Name="Line_0" Width="288" Height="197" Canvas.Left="74" <Path x:Name="Line_0" Width="288" Height="197" Canvas.Left="74"

Canvas.Top="27" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Canvas.Top="27" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Canvas.Top="27" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round" Canvas.Top="27" Stretch="Fill" StrokeThickness="30" StrokeLineJoin="Round"

Stroke="#FFFFFFFF" Data="F1 M 390,110.667L 70,3Stroke="#FFFFFFFF" Data="F1 M 390,110.667L 70,3Stroke="#FFFFFFFF" Data="F1 M 390,110.667L 70,3Stroke="#FFFFFFFF" Data="F1 M 390,110.667L 70,329.333"/>29.333"/>29.333"/>29.333"/>

</Canvas> </Canvas> </Canvas> </Canvas>

</Canvas> </Canvas> </Canvas> </Canvas>

</Button> </Button> </Button> </Button>

</Grid>

</Window>

Figure 2: Example button

Page 3: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 3

C Assigning an event to a button 7. Next modify the button code so that it now has an event, such as with:

<Button Click="ButtonOkClicked"Click="ButtonOkClicked"Click="ButtonOkClicked"Click="ButtonOkClicked">

and go to the Window1.xaml.cs file, and add:

private void ButtonOkClicked(object sender, RoutedEventArgs e)

{

MessageBox.Show("Hello");

}

8. Run the project, and show that you can click on the button and that it displays

a message box.

9. Add another button from the toolbox, and define that this is an exit button with

the code:

private void button1_Click(object sender, RoutedEventArgs e)

{

Close();

}

D UI Layout 9. Next create a ListBox with the following highlighted text:

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Napier" Height="304" Width="466">

<Grid>

<StackPanel> <StackPanel> <StackPanel> <StackPanel>

<TextBlock>My Menu</TextBlock> <TextBlock>My Menu</TextBlock> <TextBlock>My Menu</TextBlock> <TextBlock>My Menu</TextBlock>

<ListBox> <ListBox> <ListBox> <ListBox>

<ListBoxItem <ListBoxItem <ListBoxItem <ListBoxItem>>>>Napier University</ListBoxItem>Napier University</ListBoxItem>Napier University</ListBoxItem>Napier University</ListBoxItem>

<ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem>

</ListBox> </ListBox> </ListBox> </ListBox>

<RichTextBox/> <RichTextBox/> <RichTextBox/> <RichTextBox/>

</StackPanel> </StackPanel> </StackPanel> </StackPanel>

</Grid>

</Window>

10. Update the ListBox tags with the events:

<ListBoxItem Selected="Web1">Selected="Web1">Selected="Web1">Selected="Web1">Napier University</ListBoxItem>

<ListBoxItem Selected="Web2">Selected="Web2">Selected="Web2">Selected="Web2">Centre for Distributed Computing and Security

</ListBoxItem>

Page 4: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 4

11. Next add the events to the CS file:

private void Web1(object sender, RoutedEventArgs e)

{

Process.Start("IExplore.exe", "http://www.napier.ac.uk");

}

private void Web2(object sender, RoutedEventArgs e)

{

Process.Start("IExplore.exe", "http://www.cdcs.soc.napier.ac.uk/");

}

Note: make sure you add: using System.Diagnostics;

12. Run the program and make sure it works (see Figure 3).

Figure 3: Example button

13. Now add a ListBoxItem to exit the program.

D Dockpanel Layout 14. Now we can create a text box on the left hand side, and the menu on the left

with:

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Napier" Height="304" Width="466">

<Grid>

<DockPanel> <DockPanel> <DockPanel> <DockPanel>

<TextBlock DockPanel.Dock="Top">Napier</TextBlock> <TextBlock DockPanel.Dock="Top">Napier</TextBlock> <TextBlock DockPanel.Dock="Top">Napier</TextBlock> <TextBlock DockPanel.Dock="Top">Napier</TextBlock>

<ListBox DockPanel.Dock="Right"> <ListBox DockPanel.Dock="Right"> <ListBox DockPanel.Dock="Right"> <ListBox DockPanel.Dock="Right">

<L <L <L <ListBoxItem>Main Site</ListBoxItem>istBoxItem>Main Site</ListBoxItem>istBoxItem>Main Site</ListBoxItem>istBoxItem>Main Site</ListBoxItem>

<ListBoxItem>CDCS</ListBoxItem> <ListBoxItem>CDCS</ListBoxItem> <ListBoxItem>CDCS</ListBoxItem> <ListBoxItem>CDCS</ListBoxItem>

</ListBox> </ListBox> </ListBox> </ListBox>

Page 5: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 5

<RichTextBox/> <RichTextBox/> <RichTextBox/> <RichTextBox/>

</DockPanel> </DockPanel> </DockPanel> </DockPanel>

</Grid>

</Window>

15. Modify this so that it contains the links from the previous example.

16. The menu can be on the other side with:

<Window x:Class="WpfApplication2.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Grid>

<Grid.RowDefinitions> <Grid.RowDefinitions> <Grid.RowDefinitions> <Grid.RowDefinitions>

<RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/>

<RowDefinition Height="3*"/> <RowDefinition Height="3*"/> <RowDefinition Height="3*"/> <RowDefinition Height="3*"/>

</Grid.RowDefinitions> </Grid.RowDefinitions> </Grid.RowDefinitions> </Grid.RowDefinitions>

<Grid.ColumnDefinitions> <Grid.ColumnDefinitions> <Grid.ColumnDefinitions> <Grid.ColumnDefinitions>

<Co <Co <Co <ColumnDefinition/>lumnDefinition/>lumnDefinition/>lumnDefinition/>

<ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/>

</Grid.ColumnDefinitions> </Grid.ColumnDefinitions> </Grid.ColumnDefinitions> </Grid.ColumnDefinitions>

<TextBlock Grid.ColumnSpan="2">My UI</TextBlock> <TextBlock Grid.ColumnSpan="2">My UI</TextBlock> <TextBlock Grid.ColumnSpan="2">My UI</TextBlock> <TextBlock Grid.ColumnSpan="2">My UI</TextBlock>

<ListBox Background="Aqua" Grid.Row="1" Grid.Column="0"> <ListBox Background="Aqua" Grid.Row="1" Grid.Column="0"> <ListBox Background="Aqua" Grid.Row="1" Grid.Column="0"> <ListBox Background="Aqua" Grid.Row="1" Grid.Column="0">

<ListBoxItem>Napier University</ListBox <ListBoxItem>Napier University</ListBox <ListBoxItem>Napier University</ListBox <ListBoxItem>Napier University</ListBoxItem>Item>Item>Item>

<ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem> <ListBoxItem>Centre for Distributed Computing and Security</ListBoxItem>

</ListBox> </ListBox> </ListBox> </ListBox>

<RichTextBox Grid.Row="1" Grid.Column="1"/> <RichTextBox Grid.Row="1" Grid.Column="1"/> <RichTextBox Grid.Row="1" Grid.Column="1"/> <RichTextBox Grid.Row="1" Grid.Column="1"/>

</Grid>

</Window>

E Data binding 17. Create a new project with a Windows-based WPF application, and copy the

code from the following. In this case it has the name of two of the text boxes

(txtF and txtS):

<Window x:Class="WpfApplication2.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="304" Width="373">

<Grid>

<Grid.RowDefinitions> <Grid.RowDefinitions> <Grid.RowDefinitions> <Grid.RowDefinitions>

<RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/>

<RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/>

<RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/>

<RowDefiniti <RowDefiniti <RowDefiniti <RowDefinition/>on/>on/>on/>

<RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/>

</Grid.RowDefinitions> </Grid.RowDefinitions> </Grid.RowDefinitions> </Grid.RowDefinitions>

<Grid.ColumnDefinitions> <Grid.ColumnDefinitions> <Grid.ColumnDefinitions> <Grid.ColumnDefinitions>

Page 6: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 6

<ColumnDefinition Width="75"/> <ColumnDefinition Width="75"/> <ColumnDefinition Width="75"/> <ColumnDefinition Width="75"/>

<ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/>

</Grid.ColumnDefinitions> </Grid.ColumnDefinitions> </Grid.ColumnDefinitions> </Grid.ColumnDefinitions>

<Label>First Name:</Label> <Label>First Name:</Label> <Label>First Name:</Label> <Label>First Name:</Label>

<TextB <TextB <TextB <TextBox Grid.Column="1" x:Name="txtF" />ox Grid.Column="1" x:Name="txtF" />ox Grid.Column="1" x:Name="txtF" />ox Grid.Column="1" x:Name="txtF" />

<Label Grid.Row="1">Last Name:</Label> <Label Grid.Row="1">Last Name:</Label> <Label Grid.Row="1">Last Name:</Label> <Label Grid.Row="1">Last Name:</Label>

<TextBox Grid.Row="1" x:Name="txtL" Grid.Column="1" /> <TextBox Grid.Row="1" x:Name="txtL" Grid.Column="1" /> <TextBox Grid.Row="1" x:Name="txtL" Grid.Column="1" /> <TextBox Grid.Row="1" x:Name="txtL" Grid.Column="1" />

<Label Grid.Row="2">Street:</Label> <Label Grid.Row="2">Street:</Label> <Label Grid.Row="2">Street:</Label> <Label Grid.Row="2">Street:</Label>

<TextBox Grid.Row="2" Grid.Column="1" /> <TextBox Grid.Row="2" Grid.Column="1" /> <TextBox Grid.Row="2" Grid.Column="1" /> <TextBox Grid.Row="2" Grid.Column="1" />

<Label Gr <Label Gr <Label Gr <Label Grid.Row="3">City:</Label>id.Row="3">City:</Label>id.Row="3">City:</Label>id.Row="3">City:</Label>

<TextBox Grid.Row="3" Grid.Column="1" /> <TextBox Grid.Row="3" Grid.Column="1" /> <TextBox Grid.Row="3" Grid.Column="1" /> <TextBox Grid.Row="3" Grid.Column="1" />

<Label Grid.Row="4">State:</Label> <Label Grid.Row="4">State:</Label> <Label Grid.Row="4">State:</Label> <Label Grid.Row="4">State:</Label>

<TextBox Grid.Row="4" Grid.Column="1" /> <TextBox Grid.Row="4" Grid.Column="1" /> <TextBox Grid.Row="4" Grid.Column="1" /> <TextBox Grid.Row="4" Grid.Column="1" />

<Label Grid.Row="5">State:</Label> <Label Grid.Row="5">State:</Label> <Label Grid.Row="5">State:</Label> <Label Grid.Row="5">State:</Label>

<TextBox Grid.Row="5" Grid.Column= <TextBox Grid.Row="5" Grid.Column= <TextBox Grid.Row="5" Grid.Column= <TextBox Grid.Row="5" Grid.Column="1" />"1" />"1" />"1" />

<Button HorizontalAlignment="Right" Grid.Row="4" Width="75" Grid.Column="1" <Button HorizontalAlignment="Right" Grid.Row="4" Width="75" Grid.Column="1" <Button HorizontalAlignment="Right" Grid.Row="4" Width="75" Grid.Column="1" <Button HorizontalAlignment="Right" Grid.Row="4" Width="75" Grid.Column="1"

Click="Button_Click">Save</Button>Click="Button_Click">Save</Button>Click="Button_Click">Save</Button>Click="Button_Click">Save</Button>

</Grid>

</Window>

18. Next update the code for the event on the button with:

private void Button_Click(object sender, RoutedEventArgs e)

{

string firstName = this.txtF.Text;

string surname = this.txtL.Text;

MessageBox.Show("Your name is " + firstName + " " + surname);

}

19. Figure 4 shows a sample run. Modify the XAML and C# code so that it shows

the three other values in the message box.

Figure 4: Getting values

Page 7: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 7

F Template standards 20. In XAML it is possible to define new style tags. Add the highlighted code in

the following example, and prove that it defines a new style. Update it so that

it uses “Times New Roman” with non-bold text.

<Window x:Class="WpfApplication2.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Napier" Height="304" Width="373">

<Grid>

<StackPanel> <StackPanel> <StackPanel> <StackPanel>

<StackPanel.Resources> <StackPanel.Resources> <StackPanel.Resources> <StackPanel.Resources>

<Style TargetType="{x:Type <Style TargetType="{x:Type <Style TargetType="{x:Type <Style TargetType="{x:Type LabelLabelLabelLabel}">}">}">}">

<S <S <S <Setter Property="FontFamily" Value="Arial" />etter Property="FontFamily" Value="Arial" />etter Property="FontFamily" Value="Arial" />etter Property="FontFamily" Value="Arial" />

<Setter Property="FontSize" Value="12" /> <Setter Property="FontSize" Value="12" /> <Setter Property="FontSize" Value="12" /> <Setter Property="FontSize" Value="12" />

<Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontWeight" Value="Bold" />

</Style> </Style> </Style> </Style>

</StackPanel.Resources> </StackPanel.Resources> </StackPanel.Resources> </StackPanel.Resources>

< < < <LabelLabelLabelLabel>Nap>Nap>Nap>Napier University.</ier University.</ier University.</ier University.</LabelLabelLabelLabel>>>>

< < < <LabelLabelLabelLabel>Is based in Edinburgh</>Is based in Edinburgh</>Is based in Edinburgh</>Is based in Edinburgh</LabelLabelLabelLabel>>>>

</StackPanel> </StackPanel> </StackPanel> </StackPanel>

</Grid>

</Window>

G StoryBoards 21. In XAML it is possible to design a storyboard for animation. Enter the

following highlighted code, and prove that when the button is pressed that the

opacity goes from 100% to 0% in 5 seconds.

<Window x:Class="WpfApplication2.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Napier" Height="304" Width="373">

<Grid>

<Button Height="40" Width="125"> <Button Height="40" Width="125"> <Button Height="40" Width="125"> <Button Height="40" Width="125">

<Button.Triggers> <Button.Triggers> <Button.Triggers> <Button.Triggers>

<EventTrigger RoutedEvent="Button.Click"> <EventTrigger RoutedEvent="Button.Click"> <EventTrigger RoutedEvent="Button.Click"> <EventTrigger RoutedEvent="Button.Click">

<BeginStoryboard> <BeginStoryboard> <BeginStoryboard> <BeginStoryboard>

<Storyboard TargetProperty="Opacity"> <Storyboard TargetProperty="Opacity"> <Storyboard TargetProperty="Opacity"> <Storyboard TargetProperty="Opacity">

<DoubleAnimation From="1" To="0" Duration="0:0:05" /> <DoubleAnimation From="1" To="0" Duration="0:0:05" /> <DoubleAnimation From="1" To="0" Duration="0:0:05" /> <DoubleAnimation From="1" To="0" Duration="0:0:05" />

</Storyboard> </Storyboard> </Storyboard> </Storyboard>

</BeginStoryboard> </BeginStoryboard> </BeginStoryboard> </BeginStoryboard>

</EventTrigger> </EventTrigger> </EventTrigger> </EventTrigger>

</Button </Button </Button </Button.Triggers>.Triggers>.Triggers>.Triggers>

</Button> </Button> </Button> </Button>

</Grid>

</Window>

Page 8: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 8

22. Next update the code so that the button increases from 125 pixels to 300 pixels

in 5 seconds (as shown next). Modify the code so that the height increases

from 40 pixels to 80 pixels.

<Storyboard TargetProperty="Width">

<DoubleAnimation From="125" To="300" Duration="0:0:05" />

</Storyboard>

23. The following allows for a colour change of a rectangle from Red to Blue, and

back again (the source is at http://billdotnet/anim.zip). Run the program, and

prove its operation. Change the colour from Black to White, over a period of 5

seconds.

<Window x:Class="WpfApplication6.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Grid>

<StackPanel Margin="20"> <StackPanel Margin="20"> <StackPanel Margin="20"> <StackPanel Margin="20">

<Rectangle Width="94" <Rectangle Width="94" <Rectangle Width="94" <Rectangle Width="94" Height="94"> Height="94"> Height="94"> Height="94">

<Rectangle.Fill> <Rectangle.Fill> <Rectangle.Fill> <Rectangle.Fill>

<SolidColorBrush x:Name="AnimationFill" Color="Red" /> <SolidColorBrush x:Name="AnimationFill" Color="Red" /> <SolidColorBrush x:Name="AnimationFill" Color="Red" /> <SolidColorBrush x:Name="AnimationFill" Color="Red" />

</Rectangle.Fill> </Rectangle.Fill> </Rectangle.Fill> </Rectangle.Fill>

<Rectangle.Triggers> <Rectangle.Triggers> <Rectangle.Triggers> <Rectangle.Triggers>

<EventTrigger RoutedEvent="Rectangle.MouseE <EventTrigger RoutedEvent="Rectangle.MouseE <EventTrigger RoutedEvent="Rectangle.MouseE <EventTrigger RoutedEvent="Rectangle.MouseEnter">nter">nter">nter">

<BeginStoryboard> <BeginStoryboard> <BeginStoryboard> <BeginStoryboard>

<Storyboard> <Storyboard> <Storyboard> <Storyboard>

<ColorAnimation Storyboard.TargetName="AnimationFill" <ColorAnimation Storyboard.TargetName="AnimationFill" <ColorAnimation Storyboard.TargetName="AnimationFill" <ColorAnimation Storyboard.TargetName="AnimationFill"

Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:2" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:2" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:2" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:2" /> /> /> />

</Storyboard> </Storyboard> </Storyboard> </Storyboard>

</BeginStoryboard> </BeginStoryboard> </BeginStoryboard> </BeginStoryboard>

</EventTrigger> </EventTrigger> </EventTrigger> </EventTrigger>

<EventTrigger RoutedEvent="Rectangle.MouseLeave"> <EventTrigger RoutedEvent="Rectangle.MouseLeave"> <EventTrigger RoutedEvent="Rectangle.MouseLeave"> <EventTrigger RoutedEvent="Rectangle.MouseLeave">

<BeginStoryboard> <BeginStoryboard> <BeginStoryboard> <BeginStoryboard>

<Storyboard> <Storyboard> <Storyboard> <Storyboard>

<ColorAnimation Storyboard.TargetName="AnimationFill" <ColorAnimation Storyboard.TargetName="AnimationFill" <ColorAnimation Storyboard.TargetName="AnimationFill" <ColorAnimation Storyboard.TargetName="AnimationFill"

Storyboard.TargetProperty="Color" To="Red" Duration="0:0:2" /> Storyboard.TargetProperty="Color" To="Red" Duration="0:0:2" /> Storyboard.TargetProperty="Color" To="Red" Duration="0:0:2" /> Storyboard.TargetProperty="Color" To="Red" Duration="0:0:2" />

</Storyboard> </Storyboard> </Storyboard> </Storyboard>

</BeginStoryboard> </BeginStoryboard> </BeginStoryboard> </BeginStoryboard>

</EventTrigger> </EventTrigger> </EventTrigger> </EventTrigger>

</Rectangle.Triggers> </Rectangle.Triggers> </Rectangle.Triggers> </Rectangle.Triggers>

</Rectangle> </Rectangle> </Rectangle> </Rectangle>

</StackPanel> </StackPanel> </StackPanel> </StackPanel>

</Grid>

</Window>

24. Other events can be added, such as for the left hand mouse button click. Add

the following code to the previous example (Ex 23), and prove its operation.

Remeber to add it before the end Rectangle.Triggers tags.

Page 9: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 9

<EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown">

<Begin <Begin <Begin <BeginStoryboard>Storyboard>Storyboard>Storyboard>

<Storyboard> <Storyboard> <Storyboard> <Storyboard>

<DoubleAnimation <DoubleAnimation <DoubleAnimation <DoubleAnimation

Storyboard.TargetName=" Storyboard.TargetName=" Storyboard.TargetName=" Storyboard.TargetName="AnimationFillAnimationFillAnimationFillAnimationFill""""

Storyboard.TargetProperty="Opacity" Storyboard.TargetProperty="Opacity" Storyboard.TargetProperty="Opacity" Storyboard.TargetProperty="Opacity"

From="1" To="0" From="1" To="0" From="1" To="0" From="1" To="0" Duration="0:0:0.Duration="0:0:0.Duration="0:0:0.Duration="0:0:0.5" AutoReverse="True" />5" AutoReverse="True" />5" AutoReverse="True" />5" AutoReverse="True" />

</Storyboard> </Storyboard> </Storyboard> </Storyboard>

</BeginStoryboard> </BeginStoryboard> </BeginStoryboard> </BeginStoryboard>

</EventTrigger> </EventTrigger> </EventTrigger> </EventTrigger>

25. The following XAML code allows a red rectangle to be moved across the

window (http://billdotnet/rect.zip). Check the program, and make sure that it

left click mouse event on the rectangle causes it to animate, and a left click at

the end will move it back to the start.

<Window x:Class="WpfApplication6.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Grid>

<Rectangle Name="MovingRect" Fill="Red" Width="50" <Rectangle Name="MovingRect" Fill="Red" Width="50" <Rectangle Name="MovingRect" Fill="Red" Width="50" <Rectangle Name="MovingRect" Fill="Red" Width="50"

HoriHoriHoriHorizontalAlignment="Left" Margin="6,100,0,112">zontalAlignment="Left" Margin="6,100,0,112">zontalAlignment="Left" Margin="6,100,0,112">zontalAlignment="Left" Margin="6,100,0,112">

<Rectangle.Style> <Rectangle.Style> <Rectangle.Style> <Rectangle.Style>

<Style TargetType="{x:Type Rectangle}"> <Style TargetType="{x:Type Rectangle}"> <Style TargetType="{x:Type Rectangle}"> <Style TargetType="{x:Type Rectangle}">

<Style.Triggers> <Style.Triggers> <Style.Triggers> <Style.Triggers>

<EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown">

<EventTrigger.Actions> <EventTrigger.Actions> <EventTrigger.Actions> <EventTrigger.Actions>

<BeginStoryboard> <BeginStoryboard> <BeginStoryboard> <BeginStoryboard>

<Storyboard> <Storyboard> <Storyboard> <Storyboard>

<DoubleAnimation <DoubleAnimation <DoubleAnimation <DoubleAnimation

Storyboard.TargetProperty="RenderT Storyboard.TargetProperty="RenderT Storyboard.TargetProperty="RenderT Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)" ransform.(TranslateTransform.X)" ransform.(TranslateTransform.X)" ransform.(TranslateTransform.X)"

From="0" To="200" /> From="0" To="200" /> From="0" To="200" /> From="0" To="200" />

</Storyboard> </Storyboard> </Storyboard> </Storyboard>

</BeginStoryboard> </BeginStoryboard> </BeginStoryboard> </BeginStoryboard>

</EventTrigger.Actions> </EventTrigger.Actions> </EventTrigger.Actions> </EventTrigger.Actions>

</Eve </Eve </Eve </EventTrigger>ntTrigger>ntTrigger>ntTrigger>

</Style.Triggers> </Style.Triggers> </Style.Triggers> </Style.Triggers>

</Style> </Style> </Style> </Style>

</Rectangle.Style> </Rectangle.Style> </Rectangle.Style> </Rectangle.Style>

<Rectangle.RenderTransform> <Rectangle.RenderTransform> <Rectangle.RenderTransform> <Rectangle.RenderTransform>

<TranslateTransform X="0" Y="0" /> <TranslateTransform X="0" Y="0" /> <TranslateTransform X="0" Y="0" /> <TranslateTransform X="0" Y="0" />

</Rectangle.RenderTransform> </Rectangle.RenderTransform> </Rectangle.RenderTransform> </Rectangle.RenderTransform>

</Rectangle </Rectangle </Rectangle </Rectangle>>>>

</Grid>

</Window>

Page 10: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 10

26. XAML can contain key frames, where there are breakpoints in the animation.

The following move a rectangle from 0 to 350 (for seven seconds), 350 to 50

(for five seconds) and 50 to 200 (in seconds seconds). Check the operation

(http://billdotnet/rect2.zip), and then modify to that the same movement occurs

in the x-direction (instead of the x-direction).

<Window x:Class="WpfApplication6.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="500">Height="300" Width="500">Height="300" Width="500">Height="300" Width="500">

<Grid>

<Rectangle Name="MovingRect" Fill="Red" Width="50" <Rectangle Name="MovingRect" Fill="Red" Width="50" <Rectangle Name="MovingRect" Fill="Red" Width="50" <Rectangle Name="MovingRect" Fill="Red" Width="50"

HorizontalAlignment="Left" Margin="6,100,0,112"> HorizontalAlignment="Left" Margin="6,100,0,112"> HorizontalAlignment="Left" Margin="6,100,0,112"> HorizontalAlignment="Left" Margin="6,100,0,112">

<Recta <Recta <Recta <Rectangle.Triggers>ngle.Triggers>ngle.Triggers>ngle.Triggers>

<EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown">

<EventTrigger.Actions> <EventTrigger.Actions> <EventTrigger.Actions> <EventTrigger.Actions>

<BeginStoryboard> <BeginStoryboard> <BeginStoryboard> <BeginStoryboard>

<Storyboard> <Storyboard> <Storyboard> <Storyboard>

<DoubleA <DoubleA <DoubleA <DoubleAnimationUsingKeyFrames nimationUsingKeyFrames nimationUsingKeyFrames nimationUsingKeyFrames

Storyboard.TargetName="Trans" Storyboard.TargetName="Trans" Storyboard.TargetName="Trans" Storyboard.TargetName="Trans" Storyboard.TargetProperty="X" Storyboard.TargetProperty="X" Storyboard.TargetProperty="X" Storyboard.TargetProperty="X"

Duration="0:0:15"> Duration="0:0:15"> Duration="0:0:15"> Duration="0:0:15">

<LinearDoubleKeyFrame Value="350" KeyTime="0:0:7" /> <LinearDoubleKeyFrame Value="350" KeyTime="0:0:7" /> <LinearDoubleKeyFrame Value="350" KeyTime="0:0:7" /> <LinearDoubleKeyFrame Value="350" KeyTime="0:0:7" />

<LinearDoubleKeyFrame Value="50" KeyTime="0:0:5" /><LinearDoubleKeyFrame Value="50" KeyTime="0:0:5" /><LinearDoubleKeyFrame Value="50" KeyTime="0:0:5" /><LinearDoubleKeyFrame Value="50" KeyTime="0:0:5" />

<LinearDoubleKeyFrame Value="200" KeyTime="0:0:3" /> <LinearDoubleKeyFrame Value="200" KeyTime="0:0:3" /> <LinearDoubleKeyFrame Value="200" KeyTime="0:0:3" /> <LinearDoubleKeyFrame Value="200" KeyTime="0:0:3" />

</DoubleAnimationUsingKeyFrames> </DoubleAnimationUsingKeyFrames> </DoubleAnimationUsingKeyFrames> </DoubleAnimationUsingKeyFrames>

</Storyboard> </Storyboard> </Storyboard> </Storyboard>

</BeginStoryboard> </BeginStoryboard> </BeginStoryboard> </BeginStoryboard>

</EventTrigger.Actions> </EventTrigger.Actions> </EventTrigger.Actions> </EventTrigger.Actions>

</EventTrigger> </EventTrigger> </EventTrigger> </EventTrigger>

</Rectangle.Triggers> </Rectangle.Triggers> </Rectangle.Triggers> </Rectangle.Triggers>

<Rectangle.RenderTransform> <Rectangle.RenderTransform> <Rectangle.RenderTransform> <Rectangle.RenderTransform>

<TranslateTransform x:Name="Trans" X="0" Y="0" /> <TranslateTransform x:Name="Trans" X="0" Y="0" /> <TranslateTransform x:Name="Trans" X="0" Y="0" /> <TranslateTransform x:Name="Trans" X="0" Y="0" />

</Rectangle.RenderTransform> </Rectangle.RenderTransform> </Rectangle.RenderTransform> </Rectangle.RenderTransform>

</Rectangle> </Rectangle> </Rectangle> </Rectangle>

</Grid>

</Window>

G Using Microsoft Expression Blend 27. If you have Microsoft Expression Blend, create a new project. Next add a red

rectangle on the screen, and then add a new timeline from the + button on the

Object and Timeline (Figure 5). From the record line move on the 0 second

mark, move the rectangle into the centre of the design area, then move to one

second. Press the + button on the timeline (Record Keyframe). Then move to 1

second on the timeline, and move the rectangle to the left top-hand side, and

press Record Keyframe. Finally move to the 2 second mark, and move the

retangle, and press Record Keyframe. Finally go back to the 0 second

keyframe, and change the fill colour to White. There should now be a blue

dotted line for the path of the rectangle.

Page 11: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 11

Figure 5: Adding a timeline in Blend

Figure 6: Showing timeline

28. View the XAML (otherwise load it from http://billdotnet.com/rect3.zip), and

integrate it with Visual Studio, and prove its operation.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="UntitledProject2.Window1" x:Name="Window"

Title="Window1" Width="640" Height="480">

<Window.Resources>

<Storyboard x:Key="Timeline1">

Page 12: Windows Presentation Foundation Tutorial 1 - …billatnapier.com/wpf.pdf · W.Buchanan 1 Windows Presentation Foundation Tutorial 1 PDF:  Tutorial:  A Introduction

W.Buchanan 12

<ColorAnimationUsingKeyFrames BeginTime="00:00:00"

Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">

<SplineColorKeyFrame KeyTime="00:00:02" Value="#FFD31818"/>

<SplineColorKeyFrame KeyTime="00:00:04" Value="#FFD31818"/>

</ColorAnimationUsingKeyFrames>

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"

Storyboard.TargetName="rectangle"

Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransfor

m.X)">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="218"/>

<SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/>

<SplineDoubleKeyFrame KeyTime="00:00:04" Value="367"/>

</DoubleAnimationUsingKeyFrames>

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"

Storyboard.TargetName="rectangle"

Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransfor

m.Y)">

<SplineDoubleKeyFrame KeyTime="00:00:00" Value="91"/>

<SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/>

<SplineDoubleKeyFrame KeyTime="00:00:04" Value="-4"/>

</DoubleAnimationUsingKeyFrames>

</Storyboard>

</Window.Resources>

<Window.Triggers>

<EventTrigger RoutedEvent="FrameworkElement.Loaded">

<BeginStoryboard Storyboard="{StaticResource Timeline1}"/>

</EventTrigger>

</Window.Triggers>

<Grid x:Name="LayoutRoot">

<Rectangle RenderTransformOrigin="0.5,0.5" Fill="#FFFFFFFF" Stroke="#FF000000"

HorizontalAlignment="Left" Margin="51,45,0,0" x:Name="rectangle" VerticalAlignment="Top" Width="186"

Height="141">

<Rectangle.RenderTransform>

<TransformGroup>

<ScaleTransform ScaleX="1" ScaleY="1"/>

<SkewTransform AngleX="0" AngleY="0"/>

<RotateTransform Angle="0"/>

<TranslateTransform X="0" Y="0"/>

</TransformGroup>

</Rectangle.RenderTransform>

</Rectangle>

</Grid>

</Window>

29. Finally, using Microsoft Expression Blend, draw the Union Jack Flag, and

animate it so that it starts at the top-left hand side, moves to the top-right hand,

then moves to the bottom right-hand side, and finally moves back to the

original place. The flag should fade out when it reaches the top right-hand

side.