WPF: Interview Questions

What is xmlns in XAML file?

“xmlns” stands for XML namespaces. It helps us to avoid name conflicts and confusion in XML documents.

What is the difference between xmlns and xmlns:x in WPF ?

Bothe namespaces helps to define / resolved XAML UI elements. The first namespace is the default namespace and helps to resolve overall WPF elements.

The second namespace is prefixed by “x:” and helps to resolve XAML language definition.

x:Name="myStack"
x:Class

Can you explain the overall architecture of WPF?

Image for WPf Architecture

Above figure shows the overall architecture of WPF. It has three major sections presentation core, presentation framework and milcore. In the same diagram we have shown how other section like direct and operating system interact with the system. So let’s go section by section to understand how every section works.

  • User32: It decides which goes where on the screen.
  • DirectX: As said previously WPF uses directX internally. DirectX talks with drivers and renders the content.
  • Milcore: Mil stands for media integration library. This section is a unmanaged code because it acts like a bridge between WPF managed and DirectX / User32 unmanaged API.
  • Presentation core: This is a low level API exposed by WPF providing features for 2D , 3D , geometry etc.
  • Presentation framework: This section has high level features like application controls , layouts . Content etc which helps you to build up your application.

WPF object hierarchy

Object hierarchy in WPF

  • Object: As WPF is created using .NET so the first class from which WPF UI classes inherits is the .NET object class.
  • Dispatcher: This class ensures that all WPF UI objects can be accessed directly only by the thread who own him. Other threads who do not own him have to go via the dispatcher object.
  • Dependency: WPF UI elements are represented by using XAML which is XML format. At any given moment of time a WPF element is surrounded by other WPF elements and the surrounded elements can influence this element and this is possible because of this dependency class. For example if a textbox surrounded by a panel, its very much possible that the panel background color can be inherited by the textbox.
  • Visual: This is the class which helps WPF UI to have their visual representation.
  • UI Element: This class helps to implement features like events, input, layouting etc.
  • Framework element: This class supports for templating , styles , binding , resources etc. And finally all WPF controls textbox , button , grids and whatever you can think about from the WPF tool box inherits from the framework element class.

What are the different kinds of controls in WPF'?'

Control hierarchy in WPF

What are resources in WPF'?'

Resources are objects referred in WPF XAML. in WPF XAML to define resources which are nothing but objects we need to do 2 things:

  1. Import namespace
  2. Create object of the class (e.g. <custns:Customer x:Key="custobj"/>)

What is the difference between Static and Dynamic resource'?'

Static Resources: Evaluated Only once

Dynamic Resources: Evaluated every time the resource is needed

Types of Triggers in WPF

Many times we would want a style property to execute under certain conditions. Trigger’s help you to define condition under which the style values will be set.

  • Property Trigger: It is Defined under Styles. Sample:

    ```xml

    ```

    Similarly we can even Define triggers depending on multiple properties using MultiTrigger

    xml <Style x:Key="MulitTriggerButtonStyle" TargetType="Button"> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsPressed" Value="True" /> <Condition Property="Background" Value="BlanchedAlmond" /> </MultiTrigger.Conditions> <MultiTrigger.Setters> <Setter Property="Foreground" Value="Blue" /> <Setter Property="BorderThickness" Value="5" /> <Setter Property="BorderBrush" Value="Blue" /> </MultiTrigger.Setters> </MultiTrigger> </Style.Triggers> </Style>

  • Event Trigger: Can be used to Perform Action When RoutedEvent on FrameworkElement is Triggered. Event triggers can Perform Storyboard animations on Control.

    xml <Border.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource LoadStoryBoard}" /> </EventTrigger> </Border.Triggers>

  • Data Trigger:

    xml <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=Picture}" Value="{x:Null}"> <Setter TargetName="viewImage" Property="Source" Value="/Images/noImage.png" /> <Setter TargetName="viewText" Property="Text" Value="No Image Available" /> <Setter TargetName="viewText" Property="Foreground" Value="Red" /> </DataTrigger> </DataTemplate.Triggers>

  • MultiDataTrigger:

    xml <DataTemplate.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding Path=Picture}" Value="{x:Null}" /> <Condition Binding="{Binding Path=Title}" Value="Waterfall" /> </MultiDataTrigger.Conditions> <MultiDataTrigger.Setters> <Setter TargetName="viewImage" Property="Source" Value="/Images/noImage.png"/> <Setter TargetName="viewImage" Property="Opacity" Value="0.5" /> <Setter TargetName="viewText" Property="Background" Value="Brown" /> </MultiDataTrigger.Setters> </MultiDataTrigger> </DataTemplate.Triggers>

What is a Template in WPF

Template defines Look: It tells what the xaml will rendered for something(data or control)

WPF controls are lookless and you can literally make a wpf control's template anything you like.

What is ItemTemplate

ItemTemplate is a DataTemplate, ItemTemplate is just the name of the property

ItemTemplate is used to define the appearance of a data item in the ItemsSource of an ItemsControl.

Difference between Control Template and Data Template

  • Data template: can be applied based on data type dynamically at runtime
    Control Template: is tied to a specific type of contorl
  • Data template: defines the data representation i.e. different data can have different representation inside the same control
    Control Template: defines the outer structure of control

Freezable objects in WPF

An object, which has its state locked down, so that it becomes unchangeable

  • Such objects perform better
  • It is also safer if they are required to be shared between threads

What are visual and logical tree

Logical tree: The relationship between the elements which looks logical looking at the XAML is termed as Logical tree

Visual Tree: But now to display this Logical tree on to your screen you need lot of visual elements. like border, text etc. So when you add these visual elements to the logical tree that complete structure is termed as Visual Tree