Download presentation
Presentation is loading. Please wait.
Published bySamson Vowles Modified over 9 years ago
1
Introduction to Windows Presentation Foundation (WPF) Dr. Frank McCown COMP 445 – GUI Programming
2
WPF History Work began in early 2000’s by Microsoft under code name “Avalon” Effort to provide a clearer separation between the interface and business logic Avalon renamed WPF in July 2005 WPF released in 2006 as part of.NET Framework 3.0 Silverlight (released in 2007) is a subset of WPF Windows Phone 7 (released in 2010) uses Silverlight or XNA to write apps http://channel9.msdn.com/posts/Charles/Michael-Wallent-Advent-and-Evolution-of-WPF/ http://www.eweek.com/c/a/Windows/Microsoft-Gives-Avalon-Indigo-Official-Names/
3
WPF Features Works on top of DirectX to provide more advanced UI features Support for 2D and 3D graphics, audio, and video GUI appearance and content manipulated with XAML WPF controls can be embedded in Windows Forms app and vice versa Uses vector-based graphics for resolution independence WPF apps can stand alone or run in Internet Explorer
4
Bringing it all together… http://windowsclient.net/wpf/white-papers/when-to-adopt-wpf.aspx
5
Extensible Application Markup Language (XAML) Pronounced “zammel” XML-based markup language for defining and arranging GUI controls Can be manually generated/edited or edited by Visual Studio
6
Example XAML Document <Window x:Class="WPF_HelloWindows.Window1” xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Hello WPF" Height="150" Width="250"> <Label Name="label1" VerticalAlignment="Center" HorizontalAlignment="Center">Hello, WPF! Window1.xaml
7
Control Layout Windows Forms: Set absolute (x,y) position WPF: Set relative position in its layout container Controls grow/shrink as window grows/shrinks Control properties: – Width, Height – MinWidth, MinHeight, MaxWidth, MaxHeight – Margin: space around control’s edges (L,T,R,B) – HorizontalAlignment (Left, Center, Right, or Stretch) – VerticalAlignment (Top, Center, Bottom, or Stretch)
8
Alignment Introduction to WPF Layout by Christian Moser http://www.wpftutorial.net/LayoutProperties.html
9
Padding and Margin Introduction to WPF Layout by Christian Moser http://www.wpftutorial.net/LayoutProperties.html The Margin is the extra space around the control. The Padding is extra space inside the control. The Padding of an outer control is the Margin of an inner control.
10
Common Layouts Grid Canvas StackPanel DockPanel WrapPanel
11
Grid Layout Grid Panel by Christian Moser http://www.wpftutorial.net/GridLayout.html Layout is defined by a grid of rows and columns with elements placed in cells.
12
Canvas Layout Canvas Panel by Christian Moser http://www.wpftutorial.net/Canvas.html Elements are placed in relation to the distance from the top and left edges of the Canvas.
13
StackPanel Layout StackPanel by Christian Moser http://www.wpftutorial.net/StackPanel.html Elements are arranged in a single column or row.
14
DockPanel Layout Dock Panel by Christian Moser http://www.wpftutorial.net/DockPanel.html Elements are positioned based on the edge they are docked to.
15
WrapPanel Layout Wrap Panel by Christian Moser http://www.wpftutorial.net/WrapPanel.html A wrapping StackPanel where elements are wrapped to the next row or column.
16
Event Handling Press Me // Code from Windows1.xaml.cs public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { label1.Content = "Button pressed!"; }
17
Event Routing All WPF events are routed events – Can travel up (from child to parent) – Can travel down (from parent to child) Three types of routed events – Direct events: Don’t travel up or down – Bubbling events: Travel up (Source to Window) – Tunneling events: Travel down (top of containment hierarchy to Source)
18
Bubbling Event Example <GroupBox Name="myGroupBox" Header="Bubbling Example“ MouseLeftButtonUp="MyCallback"> <Label Name="myLabel" MouseLeftButtonUp=“MyCallback"> Click Me private void MyCallback(object sender, MouseButtonEventArgs e) { // Label notified of event first, then GroupBox } Bubbling event
19
Tunneling Event Example <GroupBox Name="myGroupBox" Header="Bubbling Example“ PreviewMouseLeftButtonUp ="Tunneling"> <Label Name="myLabel" PreviewMouseLeftButtonUp="Tunneling"> Click Me private void Tunneling(object sender, MouseButtonEventArgs e) { // GroupBox notified of event first, then Label } Tunneling event
20
Commands Any action or task that may be triggered by different user interactions – Example: File Exit from the menu and Exit button may both need to execute the same command Commands can better synchronize a tasks’ availability – Example: If copy command requires text to be highlighted, all controls using this command can be disabled if no text is highlighted Some common built-in commands: New, Open, Save, Close, Cut, Copy, Paste Can create your own custom commands
21
Command Example <Window x:Class="WPF_HelloWindows.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Hello WPF" Height="150" Width="250"> <Button Height="23" Margin=“80,0,80,14" Name="button1" VerticalAlignment="Bottom“ Command="Close">Press Me Called by Menu and Button
22
Styles WPF style is a collection of reusable property- value & event-handler definitions Concept is similar to style sheets for HTML Same Style might be applied to different types of controls Styles are WPF resources – Reusable objects defined for entire application
23
Styles Example 1 Same style applied to different controls
24
Styles Example 2
25
Control Templates Appearance of WPF controls determined by a control template Control template is a hierarchy of visual elements All visual controls use a default control template Changes to a control’s control template can redefine the appearance of the control without changing its functionality Button | Border | ContentPresenter | String Displays single element
26
Control Template Example <Border Name="Border" BorderThickness="2" CornerRadius="10" BorderBrush="BlueViolet"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.FontWeight="Bold" TextBlock.Foreground="Firebrick" Content="{TemplateBinding Content}" /> Button 1 <Button Name="button2" Width="100" Height="30" Template="{StaticResource MyButtonTemplate}"> Button 2
27
Triggers Triggers can be used to change a control’s appearance when it enters a certain state Can be used with Styles or Control Templates Must be defined in Style.Triggers or ControlTemplate.Triggers
28
Style with Trigger Example Push Me
29
Control Template with Trigger Example <Border Name="Border" BorderThickness="2" CornerRadius="10" BorderBrush="BlueViolet"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.FontWeight="Bold" TextBlock.Foreground="Firebrick" Content="{TemplateBinding Content}" /> <Setter TargetName="Border" Property="Background" Value="LightBlue" /> <Button Name="button2" Width="100" Height="30" Template="{StaticResource MyButtonTemplate}">Button 2 trigger
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.