WPF - Interview Questions with Answers

Windows Presentation Foundation - Questions & Answers
What is WPF?
Windows Presentation Foundation (WPF) is a next-generation presentation system for building Windows client applications with visually stunning user experiences. With WPF, you can create a wide range of both standalone and browser-hosted applications.

What is XAML?
XAML is used extensively in .NET Framework 3.0 technologies, particularly in Windows Presentation Foundation (WPF) . In WPF, XAML is used as a user interface markup language to define UI elements, data binding, eventing, and other features. In WF, workflows can be defined using XAML. XAML elements map directly to common language runtime object instances, while XAML attributes map to Common Language Runtime properties and events on those objects. XAML files can be created and edited with visual design tools such as Microsoft Expression Blend, Microsoft Visual Studio, and the hostable Windows Workflow Foundation visual designer.

What is XBAP?
XBAP stands for XAML Browser Application. XBAP allows for WPF applications to be used inside a browser. The .NET framework is required to be installed on the client system. Hosted applications run in a partial trust sandbox environment. They are not given full access to the computer's resources and not all of WPF functionality is available.WPF supports the creation of applications that run directly in a web browser. (So will WPF/E, when it is released.) They are called XAML Browser Applications (XBAPs), and have a .xbap file extension.

The power of this WPF support is that the exact same programming model is used for a XAML Browser Application as for a standard Windows application. Therefore, creating an XBAP isn’t much different than creating a standard Windows application. The maindifferences are as follows:

  • Not all features in WPF or the .NET Framework are accessible (by default).
  • Navigation is integrated into the browser (for Internet Explorer 7 or later).
  • Deployment is handled differently.

What is BAML?
BAML, which stands for Binary Application Markup Language, is simply XAML that has been parsed, tokenized, and converted into binary form. Although any chunk of XAML can be represented by procedural code, the XAML-to-BAML compilation process does not generate procedural source code. So, BAML is not like Microsoft intermediate language (MSIL); it is a compressed declarative format that is faster to load and parse (and smaller in size) than plain XAML. BAML is just an implementation detail of the XAML compilation process without any direct public exposure, so it could be replaced with something different in the future. Nevertheless, it’s interesting to be aware of its existence.

What is observableCollection in WPF?
In many cases the data that you work with is a collection of objects. For example, a common scenario in data binding is to use anItemsControl such as a ListBox, ListView, or TreeView to display a collection of records. You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes the CollectionChanged event, an event that should be raised whenever the underlying collection changes.
WPF provides the ObservableCollection(T) class, which is a built-in implementation of a data collection that implements theINotifyCollectionChanged interface. Before implementing your own collection, consider using ObservableCollection(T) or one of the existing collection classes, such as List(T),Collection(T), and BindingList(T), among many others. If you have an advanced scenario and want to implement your own collection, consider using IList, which provides a non-generic collection of objects that can be individually accessed by index. Implementing IList provides the best performance with the data binding engine.

What are the three kinds of routed events in WPF and how do they differ?

Routed events in WPF are direct, tunneling a bubbling. A direct event can be raised only by the element in which it originated. A bubbling event is raised first by the element in which it originates and then is raised by each successive container in the visual tree. A tunneling event is raised first by the topmost container in the visual tree and then down through each successive container until it is finally raised by the element in which it originated. Tunneling and bubbling events allow elements of the user interface to respond to events raised by their contained elements.

What is the difference between Navigation applicaton and XBAPs in WPF?

While both are page-based applications. Navigation Page application are used for navigation applicated hosted with full trust policy that makes them ideal for deployment in secure environment. XBAPs are generally design for Window Explorer and are not locally installed. They are partial trust application restricted to use local file system, Database, registries and other sensitive resources and this makes them ideal for wide distribution

Entry Level

  • Strong .NET 2.0 Background & willing to learn!
  • Explain dependency properties?
  • What's a style?
  • What's a template?
  • Binding
  • Differences between base classes: Visual, UIElement, FrameworkElement, Control
  • Visual vs Logical tree?
  • Property Change Notification (INotifyPropertyChange and ObservableCollection)
  • ResourceDictionary - Added by a7an
  • UserControls - Added by a7an
  • difference between bubble and tunnel routing strategies - added by Carlo


  • Routed Events & Commands
  • Converters - Added by Artur Carvalho
  • Explain WPF's 2-pass layout engine?
  • How to implement a panel?
  • Interoperability (WPF/WinForms)
  • Blend/Cider - Added by a7an
  • animations and storyboarding
  • ClickOnce Deployment
  • Skinning/Themeing
  • Custom Controls
  • How can worker threads update the UI?


  • Example of attached behavior?
  • What is PRISM,CAL & CAG?
  • How can worker threads update the UI?
  • WPF 3D - Added by a7an
  • Differences between Silverlight 2 and WPF
  • MVVM/MVP - Added by a7an
  • WPF Performance tuning
  • Pixel Shaders
  • Purpose of Freezables

When should I use WPF instead of DirectX?

DirectX is definitely not dead and is still more appropriate than WPF for advanced developers writing hard-core “twitch games” or applications with complex 3D models where you need maximum performance. That said, it’s easy to write a naive DirectX application that performs far worse than a similar WPF application. DirectX is a low-level interface to the graphics hardware that exposes all of the quirks of whatever GPU a particular computer has. DirectX can be thought of as assembly language in the world of graphics: You can do anything the GPU supports. WPF provides a high-level abstraction that takes a description of your scene and
figures out the best way to render it, given the hardware resources available. Internally, this
might involve using Shader Model 3.0, or the fixed-function pipeline, or software. (Don’t worry if you’re not familiar with these terms, but take it as a sign that you should be using WPF!).
The downside of choosing DirectX over WPF is a potentially astronomical increase in development cost. A large part of this cost is the requirement to test your application on each
driver/GPU combination you intend to support. One of the major benefits of building on top of WPF is that Microsoft has already done this testing for you! You can instead focus your
testing on low-end hardware for measuring performance. The fact that WPF applications can
even leverage the client GPU over Remote Desktop or in a partial-trust environment is also a
compelling differentiator.

When should I use WPF instead of Windows Forms?

WPF is clearly more suitable for applications with rich media, but some people have said that Windows Forms is the best choice for business applications with traditional user interfaces. I think this belief is based on first versions of WPF in which many standard controls didn’t exist (such as TreeView, ListView, and OpenFileDialog) and a visual designer didn’t exist, making traditional Windows application development in WPF harder than in Windows Forms. Although Windows Forms still has useful controls that WPF lacks (such as DataGridView and PropertyGrid) and at the time of writing has a larger set of third-party controls in the marketplace, WPF has compelling features even for traditional user interfaces (such as the support for resolution independence or advanced layout).

When should I use WPF instead of Adobe Flash?

For creating rich web content, Flash is currently the most popular option because of its ubiquity.
You can put Flash-based content on a website with confidence that the overwhelming
majority of visitors already have the necessary player installed. (And if they don’t, it’s a very
quick download.) WPF applications can also run within a web browser. WPF has the advantage of better development tools and programming model, a richer feature set, robust control reuse, broad programming language support, and full access to the underlying platform (when security
permits). But viewing such content requires Windows and the .NET Framework 3.0 (installed
by default on Windows Vista or later).

How do I get a ToolTip to appear when hovering over a disabled element?

use the ShowOnDisabled attached property of the ToolTipService class! From

XAML, this would look like the following on a Button:

Or from C# code, you can call the static method corresponding to the attached property:

ToolTipService.SetShowOnDisabled(myButton, true);

How can I forcibly close a ToolTip that is currently showing?

Set its IsOpen property to false.

When the SelectionChanged event gets raised, how do I get the new selection?

The SelectionChanged event is designed to handle controls that allow multiple selections,
so it can be a little confusing for a single-selection selector such as ComboBox. The
SelectionChangedEventArgs type passed to event handlers has two properties of type
IList: AddedItems and RemovedItems. AddedItems contains the new selection and
RemovedItems contains the previous selection.

void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
if (e.AddedItems.Count > 0)
object newSelection = e.AddedItems[0];
Like this code, never assume that there’s a selected item! Besides the fact that ComboBox’s
selection can be cleared programmatically, it can get cleared by the user when IsEditable is
true and IsReadOnly is false. In this case, if the user changes the selection box text to
something that doesn’t match any item, the SelectionChanged event is raised with an
empty AddedItems collection.

Why should I bother wrapping items in a ComboBoxItem?

ComboBoxItem exposes some useful properties—IsSelected and IsHighlighted—and
useful events—Selected and Unselected. Using ComboBoxItem also avoids a quirky behavior with showing content controls in the selection box (when IsEditable is false): If an item in a ComboBox is a content control, the entire control doesn’t get displayed in the selection box. Instead, the inner content is extracted and shown. By using ComboBoxItem as the outermost content control, the inner content is now the entire control that you probably wanted to be displayed in the first place. Because ComboBoxItem is a content control, it is also handy for adding simple strings to a ComboBox (rather than using something like TextBlock or Label).

How can I make ListBox arrange its items horizontally instead of vertically?

One way is to define a new control template, but all ItemsControls provide a shortcut with its ItemsPanel property. ItemsPanel enables you to swap out the panel used to arrange items while leaving everything else about the control intact. ListBox uses a panel called VirtualizingStackPanel to arrange its items vertically, but the following code replaces it with a new VirtualizingStackPanel that explicitly sets its Orientation to Horizontal:

The translation of this XAML to procedural code is not straightforward, but here’s how you

can accomplish the same task in C#:

FrameworkElementFactory panelFactory =

new FrameworkElementFactory(typeof(VirtualizingStackPanel));



myListBox.ItemsPanel = new ItemsPanelTemplate(panelFactory);

How can I get ListBox to scroll smoothly?

By default, ListBox scrolls on an item-by-item basis. Because the scrolling is based on each
item’s height, it can look quite choppy if you have large items. If you want smooth scrolling,
such that each scrolling action shifts the items by a small number of pixels regardless of
their heights, the easiest solution is to set the ScrollViewer.CanContentScroll attached
property to false on the ListBox. Be aware, however, that by making this change you lose ListBox’s virtualization functionality. Virtualization refers to the optimization of creating child elements only when they become visible on the screen. Virtualization is only possible when using data binding to fill the control’s items, so setting CanContentScroll to false can negatively impact the performance of data-bound scenarios only.

How do I get the items in my ItemsControl to have Automation IDs, as seen in tools like UISpy?

The easiest way to give any FrameworkElement an Automation ID is to set its Name property, as that gets used by default for automation purposes. However, if you want to give an element an ID that is different from its name, simply set the AutomationProperties.AutomationID attached property (from the System.Windows.Automation namespace) to the desired string.

How can I get items in a StatusBar to grow proportionally?

It’s common to want StatusBar panes that remain proportionately sized. For example,
perhaps you want a left pane that occupies 25% of the StatusBar’s width and a right pane
that occupies 75% of the width. This can be done by overriding StatusBar’s ItemsPanel with
a Grid and configuring the Grid’s columns appropriately.

How can I make TextBox support multiple lines of text?

Setting AcceptsReturn to true allows users to press the Enter key to create a new line of
text. Note that TextBox always supports multiple lines of text programmatically. If its Text is
set to a string containing NewLine characters, it displays the multiple lines regardless of the
value of AcceptsReturn. Also, the multiline support is completely independent from text
wrapping. Text wrapping only applies to individual lines of text that are wider than the

What unit of measurement is used by WPF?

All absolute measurements, such as the numbers used in this section’s size-related properties,
are specified in device-independent pixels. These “logical pixels” are meant to represent
1/96th of an inch, regardless of the screen’s DPI setting. Note that device-independent
pixels are always specified as double values, so they can be fractional.
The exact measurement of 1/96th of an inch isn’t important, although it was chosen
because on a typical 96 DPI display, one device-independent pixel is identical to one physical
pixel. Of course, the notion of a true “inch” depends on the physical display device. If an
application draws a one-inch line on my laptop screen, that line will certainly be longer than
one inch if I hook up my laptop to a projector!

Where’s the entry point in WPF application?

When you create a WPF Windows Application in Visual Studio, the generated project has no Main method, yet it still runs as expected! In fact, even attempting to add a Main method
gives a compilation error telling you that it is already defined.
Application is special-cased when it is compiled from XAML, because Visual Studio assigns
the XAML file a Build Action of ApplicationDefinition. This causes a Main method to be

How do I retrieve command-line arguments in my WPF application?

Command-line arguments are typically retrieved via a string array parameter passed to Main,
but the common way to define WPF applications doesn’t allow you to implement the Main
method. You can get around this in two different ways. One way is to forego defining an
Application-derived class in XAML, so you can manually define the Main method with a
string array parameter. The easier way, however, is to simply call
System.Environment.GetCommandLineArgs at any point in your application, which returns
the same string array you’d get inside Main.

Can BAML be decompiled back into XAML?

Sure, because an instance of any public .NET class can be serialized as XAML, regardless of
how it was originally declared. The first step is to retrieve an instance that you want to be the
root. If you don’t already have this object, you can call the static
System.Windows.Application.LoadComponent method as follows:
System.Uri uri = new System.Uri(“MyWindow.xaml”, System.UriKind.Relative);
Window window = (Window)Application.LoadComponent(uri);
This differs from previous code that uses FileStream to load a .xaml file because with
LoadComponent, the name specified as the Uniform Resource Identifier (URI) does not have
to physically exist as a standalone .xaml file. LoadComponent can automatically retrieve
BAML embedded as a resource when given the appropriate URI (which, by MSBuild convention,
is the name of the original XAML source file). In fact, Visual Studio’s autogenerated
InitializeComponent method calls Application.LoadComponent to load embedded BAML,
although it uses a different overload.
After you’ve gotten a hold of the root element instance, you can use the
System.Windows.Markup.XamlWriter class to get a XAML representation of the root
element (and, therefore, any of its children). XamlWriter contains five overloads of a static
Save method, the simplest of which accepts an object instance and returns appropriate
XAML as a string. For example:
string xaml = XamlWriter.Save(window);
It might sound a little troubling that BAML can be so easily “cracked open,” but it’s really no
different from any other software running locally or displaying UI locally. (For example, you can
easily dig into a website’s HTML, JavaScript, and Cascading Style Sheets [CSS] files.)

What are possible ways to implement distributed applications in .NET?

.NET Remoting and ASP.NET Web Services.

Can you create Windows Service Using WPF?

No, you can not build the windows Services using WPF. WPF is presentation technolgy, where as Windows service requires certain permission to perform the certain GUI retalted operations.
If the Windows service does not have the appropriate permissions, there may be unexpected results.

What is a Routed event?

In a typical WPF application, it contains many elements. These elements exist in an element tree relationship with each other. A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.

No comments: