Win Forms WPFLecture
Win Forms WPFLecture
Discuss the significant changes WPF introduces. There are five key principles that immediately stand out
because they are so different from previous Windows user interface toolkits such as Windows Forms. These
principles are the following:
• Hardware acceleration: All WPF drawing is performed through DirectX, which allows it to take advantage of
the latest in modern video cards.
• Resolution independence: WPF is flexible enough to scale up or down to suit your monitor and display
preferences, depending on the system DPI setting.
• Windows Form controls are placed on the form using coordinates which makes it hard to create user
interfaces that adjust properly if the window resized.
• WPF uses XAML which is a Flow Based Layout System with some support for coordinate based layout
(ie WinForms)
• Content is organized into containers that have elements added to it
• Allows for flowing dynamic layouts which lead to graceful adjusting on windows resize or display
resolution change
• In WinForms all elements have a FIXED size... if you put a button on a form and then change the size
of the form the buttons size remains the same BUT in XAML it doesnt.
• No fixed control appearance: In traditional Windows development, there’s a wide chasm between controls
that can be tailored to suit your needs (which are known as owner-drawn controls) and those that are
rendered by the operating system and essentially fixed in appearance. In WPF, everything from a basic
Rectangle to a standard Button or more complex Toolbar is drawn using the same rendering engine and
completely customizable. For this reason, WPF controls are often called lookless controls—they define the
functionality of a control, but they don’t have a hardwired “look.” For instance the WPF button is "look-less",
as are most other WPF controls, which means that it can contain a range of other controls inside of it. You
want a button with an image and some text? Just put an Image and a TextBlock control inside of the button
and you're done! You simply don’t get this kind of flexibility out of the standard WinForms controls, which is
why there's a big market for rather simple implementations of controls like buttons with images and so on.
<Button> <StackPanel Orientation="Horizontal"> <Image Source="speaker.png" Stretch="Uniform"/>
<TextBlock Text="Play Sound" /> </StackPanel> </Button>
• Declarative user interfaces: WPF is a combination of XAML (markup) and C#/VB.NET/any other .NET
language. XAML, which stands for eXtensible Application Markup Language, is Microsoft's variant of XML for
describing a GUI. In previous GUI frameworks, like WinForms, a GUI was created in the same language that
you would use for interacting with the GUI, e.g. C# or VB.NET and usually maintained by the designer (e.g.
Visual Studio), but with XAML, Microsoft is going another way. Much like with HTML, you are able to easily
write and edit your GUI.You now use XAML to define WPF user interfaces. XAML allows you to build a
window without using code. Impressively, XAML doesn’t limit you to fixed, unchanging user interfaces. You
can use tools such as data binding and triggers to automate basic user interface behavior (such as text boxes
that update themselves when you page through a record source, or labels that glow when you hover overtop
with the mouse), all without writing a single line of C#. Whether you're creating a Window or a Page, it will
consist of a XAML document and a CodeBehind file, which together creates the Window/Page. The XAML file
describes the interface with all its elements, while the CodeBehind handles all the events and has access to
manipulate with the XAML controls.
• Object-based drawing: Even if you plan to work at the lower-level visual layer (rather than the higher-level
element layer), you won’t work in terms of painting and pixels. Instead, you’ll create shape objects and let
WPF maintain the display in the most optimized manner possible.
In Short
• Probably the easiest way to understand WPF is to think of it as an assortment of objects that make it
easier to build cool user interfaces. Those objects include a new set of controls, some replacing your favorite
Windows Forms controls (such as Label, TextBox, Button, Slider) and others providing new features (such as
Expander, FlowDocument, and ViewBox).
• WPF aims to combine the best attributes of systems such as DirectX (3D and hardware
Acceleration), Windows Forms (developer productivity), Adobe Flash (powerful animation support), and
HTML (declarative markup).
• WPF applications allow user to separate appearance and user interface logic from the code behind. This is
similar to ASP.NET where web page and the code behind files are separate. In WPF, the appearance and
front end is handled by the XAML (Extensible Markup Language) and the back end is handled by any
managed programming language such as C#
Technically, the answer is “No,” just like C# and the .NET Framework don’t enable you to do something that
you couldn’t do in assembly code. It’s just a question of how much work you want to do to get the desired
results! If you were to attempt to build a WPF-equivalent application from scratch without WPF, you’d not only
have to worry about the drawing of pixels on the screen and interaction with input devices, you’d also need to
do a ton of additional work to get the accessibility and localization support that’s built in to WPF, and so on.
WPF also provides the easiest way to take advantage of Windows features, such as defining Jump List items
with a small chunk of XAML