73
Windows Presentation Foundation Max Knor Developer Evangelist Microsoft Austria http://www.knor.net

Wpf Introduction

Embed Size (px)

Citation preview

Page 1: Wpf Introduction

Windows PresentationFoundation

Max KnorDeveloper EvangelistMicrosoft Austria

http://www.knor.net

Page 2: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 3: Wpf Introduction

Demo

WPF Puzzle

Page 4: Wpf Introduction

Why WPF? Another UI Framework?

New Display EngineBased on Direct3D no window handles

Leverages Modern Hardware (GPUs)Resolution IndependenceVectorgraphics Scalable

Multimedia IntegrationControls + Documents + Video/Audio

New .NET based Development ConceptNew class librariesSeperated UI vs. Logic

XAML – XML based UI Specification language

Page 5: Wpf Introduction

Designer Developer

Developer Designer Collaboration

Page 6: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 7: Wpf Introduction

XAML

Extensible Application Markup Language

Generic XML Object Instantiation

Not How – but What!Creates Object Hierarchies

Used inWindows Presentation Foundation

Windows Workflow Foundation

Page 8: Wpf Introduction

XAML vs. C#

<Button Background="Red">Click Me!

</Button>

Button button1 = new Button();button1.Content = "Click Me!";button1.Background = new SolidColorBrush(

Colors.Red);

Page 9: Wpf Introduction

XAML vs. C#

XML Namespaces <--> .NET Namespaces

XML Elements <--> .NET Objects

XML Attributes <--> .NET Object Properties

Page 10: Wpf Introduction

Demo

XAML

Page 11: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 12: Wpf Introduction

WPF Positioning

Absolute PositioningX / Y, Width / Height

Relative PositioningNO X/Y, Width/Height

Margin

Alignment

<Button Margin="20,10,20,10">Hallo Welt</Button>

Page 13: Wpf Introduction

WPF Positioning - Alignment

HorizontalAlignmentStretch, Left, Right, Center

VerticalAlignmentStretch, Top, Center, Bottom

<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

Hallo Welt</Button>

<Button HorizontalAlignment="Left" VerticalAlignment="Bottom">

Hallo Welt</Button>

Page 14: Wpf Introduction

Layout Controls

StackPanelUnderneath, side-by-side

WrapPanelSame but with automatic wrapping

CanvasX-,Y- Positions

DockPanel

Grid

...

Page 15: Wpf Introduction

Demo

Using Layout Controls

Page 16: Wpf Introduction

Topics

Overview

XAML

Dependency Objects & Routed Events

Layout Panels

Controls

Styles, Templates & Resources

Commands

Data Binding

Page 17: Wpf Introduction

Class Hierarchy

Page 18: Wpf Introduction

Simple Controls

• PasswordBox

• ScrollBar

• ProgressBar

• Slider

• TextBox

• RichTextBox

Page 19: Wpf Introduction

Content Controls

Button

RepeatButton

ToggleButton

CheckBox

RadioButton

Label

Frame

ListBoxItem

StatusBarItem

ScrollBarViewer

ToolTip

UserControl

Window

NavigationWindow

Page 20: Wpf Introduction

Demo

Content Controls

Page 21: Wpf Introduction

Headered Content Controls

Content Control plus header

Expander

MenuItem

GroupBox

TabItem

Page 22: Wpf Introduction

Items Controls

Container for multiple items (lists)

ListBox

ComboBox

Menu

ContextMenu

StatusBar

TreeView

TabControl

...

Page 23: Wpf Introduction

Demo

Listbox, Toolbar, Menu

Page 24: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 25: Wpf Introduction

Dependency Properties

Values stored in a central dictionaryInstead of being stored in a private member

Can include additional metadata (like default value)

Manipulated by the WPF runtimeDatabinding

Animation, Styles, etc…

Page 26: Wpf Introduction

Dependency Properties

public class MyDependencyObject : DependencyObject{

public static readonly DependencyPropertySomeStateProperty = DependencyProperty.Register("SomeState",

typeof(String), typeof(MyDependencyObject));

public string SomeState{

get { return (string)this.GetValue(SomeStateProperty); }set { this.SetValue(SomeStateProperty, value); }

}}

Page 27: Wpf Introduction

Attached Properties

Store Properties of one control on another one

<Grid><Button Grid.Column=“0" Grid.Row="40">

Click Me!</Button>

</Grid>

Page 28: Wpf Introduction

Demo

Dependency Properties

Attached Properties

Page 29: Wpf Introduction

Routed Events

Application

Window

Grid

Button

Canvas

Ellipse

Preview

Preview

Preview

Preview

Preview

Preview

Tunnelin

gBubblin

g

Page 30: Wpf Introduction

Routed Events (Attached Events)

“Collect” events at a higher hierarchy level

<Canvas Button.Click=“Button_Click”><Button>

Click Me!</Button>

</Canvas>

Page 31: Wpf Introduction

Events

Create Routed Events

Use RoutedEventArgs.Source instead of sender

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler),

typeof(MyButtonSimple));

public event RoutedEventHandler Tap { add { AddHandler(TapEvent, value); }remove { RemoveHandler(TapEvent, value); }

}

Page 32: Wpf Introduction

Demo

Routed Events

Page 33: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 34: Wpf Introduction

Designer Developer

Page 35: Wpf Introduction

Styles are about setting properties…

Page 36: Wpf Introduction

Demo

Basic Styles

Resources

Page 37: Wpf Introduction

Resource Hierarchy

System Resources

Application

Resources

Element

Resources

Element

Resources

Element

Resources

Window/Page

Resources

Window/Page

Resources

Element

Resources

Application

Resources

<Window><Window.Resources>...

</Window.Resources>

<Grid><Grid.Resources>...

</Grid.Resources>

<ListBox><ListBox.Resources>...</ListBox.Resources>

</ListBox></Grid>

</Window>

Page 38: Wpf Introduction

Automatic Styles/Templates

Most resources must use x:Key

Optional with Styles and Data TemplatesCan use TargetType or DataType instead

Applied to target automatically – no reference required

<Window.Resources><Style TargetType="{x:Type TextBlock}"><Setter Property="Margin" Value="5" />

</Style></Window.Resources>

Page 39: Wpf Introduction

Lookless Controls and Templates

Control implies behaviour

Probably supplies default lookDesigner free to supply new look

Page 40: Wpf Introduction

Demo

Control Templates

Page 41: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 42: Wpf Introduction

Data Binding

Concept

Binding SourceOn<PropertyName>Changed

INotificationPropertyChanged

DependencyProperty

Page 43: Wpf Introduction

Binding in Markup

<Image Source="truck.png"Canvas.Left=

"{Binding Path=Value, ElementName=horzPos}"

/>

<Slider Orientation="Horizontal"Name="horzPos"Value="40"/>

{Binding Path=Value, ElementName=horzPos}

Page 44: Wpf Introduction

Demo

UI Element Binding

Object Binding

Page 45: Wpf Introduction

Share Common Source

StackPanel

Image

HorizontalSlider

Value={Binding Path=XPos,Source={StaticResource myData}}

Canvas.Left={Binding Path=XPos,

Source={StaticResource myData}}

DataContext={Binding Source={StaticResource myData}}

Value={Binding Path=XPos}

Canvas.Left={Binding Path=XPos}

Page 46: Wpf Introduction

Provide Data From Code

May be easier to load data in codebehind

Can set DataContext in code

Foo myDataObject = new Foo();myDataObject.Bar = 42;

// Set DataContextmyWindow.DataContext = myDataObject;

Page 47: Wpf Introduction

Data Binding – Conversion & Validation

ValidationValidationRule

ConverterIValueConverter,

IMultiValueConverter

Page 48: Wpf Introduction

Demo

Databinding to CLR Objects

Page 49: Wpf Introduction

Data and Controls

ContentControl – singular contentButton, CheckBox, Label, ListBoxItem, …

ItemsControl – plural contentListBox, ComboBox, Menu, TabControl, ToolBar, …

Can use data objects as content

myListBox.Items.Add(new Car());

myButton.Content = new Car();

Car c = (Car) myListBox.SelectedItem;

Page 50: Wpf Introduction

class Car{string Image {get;set}string Model {get;set}

}

Data Templates

DataTemplate

<DataTemplate x:Key="carTemplate"><Border BorderBrush="Blue" BorderThickness="2" Background="LightGray"

Margin="10" Padding="15,15,15,5"><StackPanel><Image HorizontalAlignment="Center" Source="{Binding Path=Image}" /><Border HorizontalAlignment="Center" BorderBrush="Navy"

Background="#DDF" BorderThickness="1" Margin="10" Padding="3"><TextBlock FontSize="18" Text="{Binding Path=Model}" />

</Border></StackPanel>

</Border></DataTemplate>

Page 51: Wpf Introduction

Demo

Databinding a list

Datatemplates

Page 52: Wpf Introduction

Topics

Overview

XAML

Layout Panels

Controls

Dependency Objects & Routed Events

Styles, Templates & Resources

Data Binding

Page 53: Wpf Introduction

Have a look at

XAML Powertoys(http://karlshifflett.wordpress.com/xaml-power-toys/)

WPF Toolkit – Datagrid, DatePicker, Ribbon(www.codeplex.com/wpf/)

WPF Themes(www.codeplex.com/wpfthemes/)

Silverlight Toolkit(www.codeplex.com/silverlight/)

Page 54: Wpf Introduction

Contact

Max Knor

Developer Evangelist

Microsoft Austria

http://www.knor.net

Page 55: Wpf Introduction
Page 56: Wpf Introduction

WPF Commands

Page 57: Wpf Introduction

WPF Commands

GOF Command Pattern

Decoupling control and command

ICommand interface

Execute(), CanExecute()

Implemented by RoutedUICommand

Source implements ICommandSource

Page 58: Wpf Introduction

Commands Architecture

Page 59: Wpf Introduction

Defining Commands

Controls define a command

CommandBindings define the handler

<Button Command="ApplicationCommands.New"><Image Source="toolbargraphics/New.bmp" />

</Button>

<Window.CommandBindings><CommandBinding

Command="ApplicationCommands.New" Executed="FileNew" CanExecute="CanFileNew" />

</Window.CommandBindings>

Page 60: Wpf Introduction

Command Libraries

Predefined Command LibrariesApplicationCommands

ComponentCommands

EditingCommands

MediaCommands

NavigationCommand

Page 61: Wpf Introduction

Demo

CommandBindings

Page 62: Wpf Introduction

Custom Commands

Instantiate an RoutedUICommand

Assign InputGestures

Pack into CommandLibrary

Page 63: Wpf Introduction

Demo

Custom Command

Page 64: Wpf Introduction

Interop and Migration

Page 65: Wpf Introduction

Interoperability!

Can use WPF with existing codeWPF inside existing code

Existing code inside WPF

Interop at the component level

Maximum richness => all WPF

Page 66: Wpf Introduction

WPF

File Edit View Help

Win32

DirectX

WPF

File Edit View Help

Win32

DirectX

Airspace

One pixel, one technology

Page 67: Wpf Introduction

File Edit View Help

Win32

DirectX

WPF

Airspace

File Edit View Help

Win32

WPF

DirectX

Page 68: Wpf Introduction

Chrome and Canvas

Canvas

Chrome

Page 69: Wpf Introduction

Mixed Application Ideas

A new look for your chrome

Visual effects for your canvas

WPF for faster rendering?

Wizards and help systems

Generate HTML => Generate XAML

Page 70: Wpf Introduction

Windows Forms

private void WindowLoaded(object sender, EventArgs e){

WindowsFormsHost host = new WindowsFormsHost();host.Height = new Length(120);host.Width = new Length(150);swf.Control child = new UserControl1();child.Dock = swf.DockStyle.None;host.AddChild(child);border.Child = host;

}

Page 71: Wpf Introduction

Demo

WPF <--> Windows Forms Interop

Page 72: Wpf Introduction

Summary

Can use WPF with existing code

Maximum richness => all WPF

Interop is for components

Page 73: Wpf Introduction