Tuesday, 25 October 2011

Learn WPF in one Week

I started to build a one week training course to learn WPF. Starting with the basics and going through the important concepts of the framework up to more complex topics. I hope you like it.

Day 1 - Getting started

Day 2 - Concepts of WPF

Day 3 - Layout and Controls

Day 4 - DataBinding and UI Architecture

Day 5 - Templates and Styles

Some of the articles are not yet finished. The content for the subsequent days will follow soon... :-)

How to create a simple WPF application

In Visual Studio 2008

Open Visual Studio 2008 and choose "File", "New", "Project..." in the main menu. Choose "WPF Application" as project type.
Choose a folder for your project and give it a name. Then press "OK"
Visual Studio creates the project and automatically adds some files to the solution. A Window1.xaml and an App.xaml. The structure looks quite similar to WinForms, except that the Window1.designer.cs file is no longer code but it's now declared in XAML as Window1.xaml
Open the Window1.xaml file in the WPF designer and drag a Button and a TextBox from the toolbox to the Window
Select the Button and switch to the event view in the properties window (click on the little yellow lightning icon). Doubleclick on the "Click" event to create a method in the codebehind that is called, when the user clicks on the button.
Note: If you do not find a yellow lightning icon, you need to install the Service Pack 1 for VisualStudio on your machine. Alternatively you can doubleclick on the button in the designer to achieve the same result.
Visual Studio automatically creates a method in the code-behind file that gets called when the button is clicked.
 
private void button1_Click(object sender, RoutedEventArgs e)
{
    textBox1.Text = "Hello WPF!";
}
 
 
The textbox has automatically become assigned the name textBox1 by the WPF designer. Set text Text to "Hello WPF!" when the button gets clicked and we are done! Start the application by hit [F5] on your keyboard.
Isn't this cool!

Videos about WPF

Books about WPF

Brand New!

WPF 4 - Unleashed

In my opinion one of the best books about WPF - now updated to version 4.0. It's printed fully in color, so all code samples have syntax highlighting. It includes all the good explanations and samples of version 3 including new chapters about multitouch, XAML 2009, VSM, improved text rendering, windows 7 shell integration and more.
Autor: Adam Nathan
ISBN: 978-0672331190
Published: June 2010

Find it at Amazon

WPF Control Development Unleashed

In this book, two leading Windows Presentation Foundation experts give developers everything they need to build next-generation WPF applications--software that is more robust, usable, and compelling. Drawing on their close ties with Microsoft's WPF development team, Pavan Podila and Kevin Hoffman give you a clear, robust, and practical understanding of WPF, its underpinnings, its overall architecture, and its design philosophy.
Autor: Pavan Podila, Kevin Hoffman
ISBN: 978-0672330339
Published: September 2009

Find it at Amazon

Windows Presentation Foundation - Unleashed     (My favorite!)

In my opinion one of the best book about WPF. It covers all important themes including 3D programming. All code samples have syntax highlighting. Digging deeper sections with additional informations for advanced programmers.
Autor: Adam Nathan
ISBN: 978-0672328916
Published: January 2007

Find it at Amazon

Application = Code + Markup

"Get the definitive guide to the Windows Presentation Foundation (WPF), the new client programming interface for the Microsoft .NET Framework 3.0 and Windows Vista. Award-winning author Charles Petzold teaches you how to combine C# code and the Extensible Application Markup Language (XAML) to develop applications for the WPF.". I cannot unterstand how anyone can write a book about a UI technology without a single illustration. If you prefer gray text to colorful images, it might be the book for you.
Autor: Charles Petzold
ISBN: 978-0735619579
Published: August 2006

Essential Windows Presentation Foundation

Chris Anderson was one of the chief architects of the next-generation GUI stack, the Windows Presentation Framework (WPF), which is the subject of this book. Chris's insights shine a light from the internals of WPF to those standing at the entrance, guiding you through the concepts that form the foundation of his creation.
Autor: Chris Anderson
ISBN: 978-0321374479
Published: April 2007

Foundations of WPF: An Introduction to Windows Presentation Foundation

Foundations of WPF: An Introduction to Windows Presentation Foundation teaches you everything you need to get started with the technology, and is broken into three parts. The first introduces and contextualizes the WPF technology; the second part dives deeper into the facets of the technology that are of immediate and valuable use in developing applications; the last part offers you the real-world perspective you need to be productive in the community and customer base.
Autor: Laurence Moroney
ISBN: 978-1590597606
Published: November 2006

Pro WPF in C# 2008

This book explains how WPF works from the ground up. It follows on from the author s previous and highly successful books, covering Windows Forms (WPF's predecessor technology) and earlier versions of WPF. It is a one-stop shop in Apress proven Pro style that leaves readers with a deep understanding of the technology and able to take the concepts away and apply them for themselves.
Autor: Matthew McDonald
ISBN: 978-1590599556
Published: Februar 2008

Foundations of WPF: An Introduction to Windows Presentation Foundation

If you want to build applications that take full advantage of Windows Vista's new user interface capabilities, you need to learn Microsoft's Windows Presentation Foundation (WPF). This new edition, fully updated for the official release of .NET 3.0, is designed to get you up to speed on this technology quickly.
Autor: Chris Sells and Ian Griffiths
ISBN: 978-0596510374
Published: September 2007

WPF Development Tools

Microsoft provides two development tools for WPF applications. One is Visual Studio, made for developers and the other is Expression Blend made for designers. While Visual Studio is good in code and XAML editing, it has a rare support for all the graphical stuff like gradients, template editing, animation, etc. This is the point where Expression Blend comes in. Blend covers the graphical part very well but it has (still) rare support for code and XAML editing.
So the conclusion is that you will need both of them.

Microsoft Visual Studio 2010

Visual Studio is the tool for developers to develop WPF applications. It includes a graphical designer for WPF since version 2008. If you're using Visual Studio 2005 you can install an add-on that enables you to develop WPF applications.
Microsoft provides a free Express Edition of Visual Studio that also includes the WPF designer. You can download it from the following URL
Download Microsoft Visual C# 2010 - Express Edition


Microsoft Expression Blend 3 + Sketch Flow

Expression Blend is the tool for designers. It's part of the Expression Studio, a new tool suite from Microsoft especially created for user experience designers. Blend covers all the missing features of the Visual Studio designer like styling, templating, animations, 3D graphics, resources and gradients.
In the latest Version it also includes a powerful prototyping tool called SketchFlow. Expression Blend can open solution files created by visual studio.
Download Microsoft Expression Blend 3


Other useful tools

  • WPF Inspector
  • Snoop (Inspect the Visual Tree of running WPF applications)
  • Mole (Data Visualizer for Visual Studio
  • XAML Power Toys
  • WPF Performance Suite

Introduction to Windows Presentation Foundation

Overview

The Windows Presentation Foundation is Microsofts next generation UI framework to create applications with a rich user experience. It is part of the .NET framework 3.0 and higher.
WPF combines application UIs, 2D graphics, 3D graphics, documents and multimedia into one single framework. Its vector based rendering engine uses hardware acceleration of modern graphic cards. This makes the UI faster, scalable and resolution independent.
The followinig illustration gives you an overview of the main new features of WPF

Separation of Appearance and Behavior

WPF separates the appearance of an user interface from its behavior. The appearance is generally specified in the Extensible Application Markup Language (XAML), the behavior is implemented in a managed programming language like C# or Visual Basic. The two parts are tied together by databinding, events and commands. The separation of appearance and behavior brings the following benefits:
  • Appearance and behaviour are loosely coupled
  • Designers and developers can work on separate models.
  • Graphical design tools can work on simple XML documents instead of parsing code.

Rich composition

Controls in WPF are extremely composable. You can define almost any type of controls as content of another. Although these flexibility sounds horrible to designers, its a very powerful feature if you use it appropriate. Put an image into a button to create an image button, or put a list of videos into a combobox to choose a video file.


 
<Button>
    <StackPanel Orientation="Horizontal">
        <Image Source="speaker.png" Stretch="Uniform"/>
        <TextBlock Text="Play Sound" />
    </StackPanel>
</Button>
 
 

Highly customizable

Because of the strict separation of appearance and behavior you can easily change the look of a control. The concept of styles let you skin controls almost like CSS in HTML. Templates let you replace the entire appearance of a control.
The following example shows an default WPF button and a customized button.

Resolution independence

All measures in WPF are logical units - not pixels. A logical unit is a 1/96 of an inch. If you increase the resolution of your screen, the user interface stays the same size - it just gets crispier. Since WPF builds on a vector based rendering engine it's incredibly easy to build scaleable user interfaces.