We are starting a new series on WPF Interview questions for reference.
1- What is the base class in WPF (Not .net framework base class)?
a- DispatherObject
b- DependencyObject
c- UIElement
d- Visual
Ans :
Base class is DispatcherObject. This class is used to provide Thread
Affinity features of the WPF controls and Thread affinity needed to overcome
the complex global locking mechanism.
2- Which Layout in WPF is most performant?
a - Grid
b- Canvas
c- StackPanel
d- All of the above
Ans : Canvas , since we already know the position where to
place the control so position calculation in layout phases are not
required.
3- In Layout phase of WPF, how many passes of Logical Tree happened?
a- 1
b- 2
3- 0
4- 4
Ans : 2 , Measure
and Arrange Phase .
5- Which tree traversal algorithm will be used by Layout sub system?
a- DFS
b- BFS
c- Any of the above
d- No specific algorithm
Ans : A DFS as mentioned above in
question 4
6- How many UI thread can theoretically exists in WPF?
a- 1
b- 2
c- 4
d- As many as you want
Ans : As many as you want , By default only one UI thread which happens
to be main thread will be created by WPF runtime.
7- Suppose you want to create a Custom control in WPF and want to
provide LIFE features (Layout, Input, Focus and Events), which base class
closely matches your requirement?
a- UIElement
b- Visual
c- FrameworkElement
d- Control
Ans : UIElement is
matches the closet LIFE.
8-Will there be 1:1 mapping between Dispatcher and Current
Thread?
a- Yes
b- No
Ans : Yes , each UI
thread will have its own dispatcher.
9- Where is the Dispatcher of the thread saved?
a- In TLS ( thread local storage)
b- In a system wide Static List
c- Dispather will not be saved inside thread
d- In a system wide Static Dictionary
Ans : in a system wide
static list/
10- If Dependency properties are static then how individual
control gets the different value?
Ans : Dependency properties are static i.e. each
control will have same dependency property declaration and this declaration is
saved inside a Hashtable with key as dependency property name and value as
dependency Property itself . This hashtable will only holds the name and the dp
object and not the DP value. So where the value
does gets stored.
For this, we need to see the DependencyObject (base class from which
underlying control is derived from)
Dependency object maintains a collection of the values (EffectiveValues),
and GetValue method of the DependencyObject is going to find the value first
from the Control instance and if not than moving up to the logical tree and
check for the property value.
While setting , it first check whether caller has sent
DependencyProperty.UnsetValue while calling SetValue , if yes than it will just
Clear the value and return or otherwise it will check whether the Value already
exist
12- Suppose you want to implement Hit Testing feature in a custom
control which you are writing? Which top level base class closely matches your
requirement and you want to inherit from?
1- Dependency Object
2- UI Element
3- Visual
4- Framework Element
Ans : Visual class
13- Which design pattern from Gang of Four closely matches with Attached
Properties in WPF and Why?
Ans : Its Decorator Design Pattern because Attached Properties are also
adding behavior at run time to the objects
14 - Can you add click event to the grid?
a- Yes, just create a custom control which mimics the functionality of
GRID and BUTTON.
b-
No, its not possible
c-
Yes, By Attached Properties
d-
Yes, By Attached Events
Ans : Yes, Attached Events
15- Relative Source Binding is possible due to the Visual Tree?
a-Yes
b-
No, There is no relative source binding
Ans : Yes,Visual tree participate in the relative source binding because
Visual tree actually contains the elements added for Visual support as well as
elements defined in the logical tree.
16 - If you want to modify a control which is owned by secondary thread
from UI Thread, how will you do it?
a- By Using Current Thread’s Dispatcher, if no dispatcher is there, WPF
runtime will create one
b- By using main thread's Dispatcher, if no dispatcher is there, WPF
runtime will create one
c- By Using Current Thread’s Dispatcher, if no dispatcher is there,
programmer can create one
d- You cannot do it, control created must be owned by UI thread
Ans :D, Although you can create a UI control in separate thread but
you need to access it
17- Does Background Worker class bypass Dispatcher mechanism while
updating a control owned by UI thread?
Ans : No because Background worker class internally call the Dispatcher
to process input on UI.
18- Which Feature of WPF mostly makes MVVM so compatible with various
Design principle ( e.g. SOLID)?
Ans – Data binding as it eliminates the need of writing getter and setter
code in “.xaml.cs”
19- Suppose you are working on a GPS application and you get lat , long
of a moving object and you want to provide visually the position of the object
, which layout panel is best suited for you?
a- Gird
b- Dock panel
c- Vistulizing
Stack Panel
d- Canvas.
Ans : Canvas , since we know the position , so there is no need to
calculate in Layout process.
20- Can you handle the mouse move / drag and drop event in ViewModel if
you are following MVVM design pattern if yes than how?
Ans : Yes, you need to use EventTrigger or Behavior for this
which are found in System.Windows.Intractivity dll.
21- In a true strict MVVM implementation (where you wont find
.xaml.cs file), can you use RoutedCommand ? If yes How , If No then Why?
Ans : No because RoutedCommand need to find the CommandBinding by walking
through the LogicalTree and command binding must specify a routed event handler
which has to be handled in .xaml.cs file.
22- How will you batch the Notifications of ObservableCollection
i.e. Suppose you want to update the ItemsControl after every 10 changes , how
will you acheive it in a strict Object Oriented Way?
Ans : By Subclassing the ObservableCollection class and you will get
access to the protected "Items" collection of the
ObservableCollection and then just override the Add method to provide the batch
support.