Windows Presentation Foundation (WPF)
- It is a new graphical subsystem that provides the functionality to create next generation Windows applications.
- It combines user interface, 2D and 3D graphics, documents, and multimedia.
- It is an API for building rich Windows client applications with visually stunning user experiences.
- It contains the capabilities of preceding technologies such as MFC, Windows Forms including GDI, GDI+, HTML, and etc.
Features
XAML (Extensible Application Markup Language)
XAML is an XML based language used for designing presentation logic in WPF. With the declarative programming model enabled by XAML, the presentation logic and business logic can be split. The designer can use XAML to design the page. After finishing, the design of page can be handed to a developer for integration with his or her business logic, it is more readable yet shorter than the code.
Rich Control Set
WPF has plenty of standard sets of controls that enable quick assembling of traditional user interfaces. Most of the controls are similar to Win.forms controls with little additional changes, as shown below.
- ListBox
In WinForm, the list box can have only text values. It cannot display a list of images with the text next to it.
Example of List box in WPF
- <ListBox Margin=”5,0,5,0” selectionMode=”Multiple” Name=”lstDemmy”>
- <StackPanel Orientation=”Horizontal”>
- <Image Source=”Happyface.jpg” Width=”30” Heght=”30” />
- <Label VerticalContentAlignment=”Center” Content=”A Happy Face” /> </StackPanel>
- <StackPanel Orientation=”Horizontal”>
- <Image Source=”redex.jpg” Width=”30” Heght=”30” />
- <Label VerticalContentAlignment=”Center” Content=”A Warning sign” /> </StackPanel>
- </ListBox>
- CheckBox/RadioButton
Both, WinForm and WPF, provide checkbox and radio button controls to give the user an option, such as true/false. WPF supports tooltip property while the controls in WinForm do not support such property. Additionally, the tooltip can be customized to include text and image or a multiline text.
Example in WPF
- <CheckBox Grid.Column=”0” Grid.Row=”3”>
- <CheckBox.ToolTip>
- <StackPanel>
- <Label FontWeight=”Bold” Content=”The Check Box” />
- <TextBlock Padding=”10” TetWrapping=”wrapWithOverflow” Width=”200”> Check Box with a fancy Tip Control </TextBolck>
- <Line Stroke=”Black” StrokeThickness=”1” X2=”200” />
- <StackPanel Orientation=”Horizontal”>
- <Image Margin=”3” Source=”help.gif”/>
- <Labe Content=”Press F1 for more help.”/> </StackPanel>
- </StackPanel>
- </CheckBox.ToolTip>
- </CheckBox>
The CheckBoxes or Radio buttons in WPF or WinForms can be grouped using GroupBox container. The WPF provides an additional control called Expander. This Expander also like Group Box, but contains a button that enables expanding and collapsing the inner content. The expander can be used as shown markup below.
- <Expander Header=”Subject” Grid.ColumnSpan=”4” Grid.RowSpan=”1” Margin=”0,5,5,5”>
- <StackPanel>
- <CheckBox Content=”English” />
- <CheckBox Content=”Maths” />
- <CheckBox Content=”Computer Science” /> </StackPanel>
- </Expander>
- CheckBox List Control
WPF allows adding multiple checkbox list controls inside ListBox. Additionally, the layout of these checkbox list controls can also be controlled by using WPF standard layouts.
Example,
- <ListBox Name=”LstChkLst”>
- <CheckBox Margin=”5”>Windows Presentation Foundation</CheckBox>
- <CheckBox Margin=”5”>Windows Communication Foundation</CheckBox>
- <CheckBox Margin=”5”>Windows Workflow Foundation</CheckBox>
- </ListBox>
- ComboBox
The comboBox supports binding to in-memory collection or ADO.NET objects. They always display a single text value. They cannot display images or image with text like ListView control. However, WPF provides a ComboBox that helps display items where these items make the ComboBox look like a Microsoft Office-style “gallery”, showing a preview and description for each item as shown in the markup below.
- <ComboBox>
- <!-- ITEM #1 -->
- <StackPanel Orientation=”Horizontal” Margin=”5”>
- <Image Source=”Image1.png” />
- <StackPanel Width=”200”>
- <TextBlock Margin=”5,0,5,0” FontSize=”14” fontWeight=”Bold”>WPF Styles</TextBlock>
- <TextBlock Margin=”5,0,5,0” FontSize=”14” fontWeight=”Bold”>WPF Styles, it is collection of properties and actions </TextBlock>
- </StackPanel>
- </stckPanel>
- <!-- ITEM #2 -->
- <StackPanel Orientation=”Horizontal” Margin=”5”>
- <Image Source=”Image1.png” />
- <StackPanel Width=”200”>
- <TextBlock Margin=”5,0,5,0” FontSize=”14” fontWeight=”Bold”> Control Template </TextBlock>
- <TextBlock Margin=”5,0,5,0” FontSize=”14” fontWeight=”Bold”>Control Template defines the appearance of the control. </TextBlock>
- </StackPanel>
- </stckPanel>
- </ComboBox>
- Button Control
The shape of the Button control cannot be changed in Win Form, but WPF can create buttons that are elliptical in shape or button with shaded background and foreground.
- TextBox
Similar to WinForm application, WPF also provides a TextBox that helps display editable text control. And WPF control additionally supports spell check by using SpellCheck.IsEnabled=”True”.
WPF Resources
WPF Resource means to define and reuse frequently required objects and values called resources. These resources are often styles or bindings. It can be defined at application level, window level, and container level or at control level.
There are Two types of Resources,
- StaticResource
A resource defined and used at design time.
- DynamicResources
A resource that provides a value for a property by referencing the resource values at run time.
WPF Styles
Styles are sets of properties and actions to associate with an element. These styles are similar to CSS styles. Styles in WPF can do more than CSS style can. Based on user interaction or on content, they can make the control look or behave differently, using triggers. Styles can be defined declaratively in XAML Using <Style> with <Setter> tag or programmatically using Style class instance.
WPF Templates
WPF supports managing the look and feel of a control that is a look-less control. A template, allows completely replacement of an element’s visual tree with anything that can be required, while keeping all of its functionality intact.
There are two types of templates
- Control Template
It allows Changing the structure and appearance of a control by defining a new control template for the control For example, it can be used for creating an elliptical button instead of dull gray rectangular button.
- Data template
It is used to define the visual representation of data. It determines what the data looks like in the rendered application. it is a XAML markup that defines how a bound data object should be displayed.
Layout Panels
Layout Panels are used to position the elements within a window.
- Canvas
It is the most basic panel. The elements can be positioned in a Canvas using its attached properties: Left, Top, Right , and Bottom.
- StackPanel
It is the panel that allows stacking its child elements sequentially either horizontally or vertically.
- WrapPanel
It is similar to StackPanel. In addition to stacking its child elements, it wraps them to additional rows or columns when there is not enough space for a single stack. This is useful for displaying an indeterminate number of items with a more interesting layout than asimple list, much like Windows Explorer does.
- DockPanel
It enables easy docking of elements to an entire side of the panel, stretching it to fill the entire width or height. It also enables a single element to fill all the remaining s space unused by the docked elements. It has a Dock attached property, so child elements can control their docking with one of four possible values: Left, Top, Right, and Bottom.
- Grid
It enables arranging elements in a tabular fashion and provides a number of features to control the rows and columns in interesting ways
Audio and Video
WPF supports media integration using audio/video files. It supports wide variety of media files like Windows Media Video (.WMV), MPEG, AVI.
For example,
- <MediaElement Source=”album\songEx.WMV” width=”400” Height=”300” />
WPF Interoperability
WPF lacks some features that Win Forms already had such as WPF does not include controls like report viewer, DateTimePicker, MonthCalender, NumericUpDown, MaskedTextBox, NotifyIcon, dataGridView etc., and MDI applications. For this WPF provides a solution to meet this requirement in the form of System.Windows.Forms.Integration namespace that Provides two key classes,
WindowsFormsHost and ElementHost. A WPF application permits hosting Windows Forms Controls in WPF using WindowsFormsHost. A Windows Forms can host a WPF element by using ElementHost Class.
WPF supports documents
WPF supports documents like
- Fixed Documents
It maintains the precise positional placement of content elements independent of the display or print device in use.
- XML Paper Specification (XPS) Document
It is a cross platform, XML schema based, electronic paper that can be read by humans, hardware and software.
- Flow Document
It is designed to reflow content depending on the container or the device where it is displayed.