WPF Interview Questions

Question: What is the base class in WPF (Not .net framework base class)?

  1. DispatherObject
  2. DependencyObject
  3. UIElement
  4. Visual

Answer: 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.


Question: Which Layout in WPF is most performant?

  1. Grid
  2. Canvas
  3. StackPanel
  4. All of the above

Answer: Canvas , since we already know the position where to place the control  so position calculation in layout phases are not required.


Question: In Layout phase of WPF, how many passes of Logical Tree happened?

  1. 1
  2. 2
  3. 0
  4. 4

Answer: 2, Measure and Arrange Phase.


Question: Which tree traversal algorithm will be used by Layout sub system?

  1. DFS
  2. BFS
  3. Any of the above
  4. No specific algorithm

Answer: A DFS.


Question: How many UI thread can theoretically exists in WPF?

  1. 1
  2. 2
  3. 4
  4. As many as you want

Answer: As many as you want, By default only one UI thread which happens to be main thread will be created by WPF runtime.


Question: 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?

  1. UIElement
  2. Visual
  3. FrameworkElement
  4. Control

Answer: UIElement is matches the closet LIFE.


Question: Will there be 1:1 mapping between Dispatcher and Current Thread?

  1. Yes
  2. No

Answer: Yes , each UI thread will have its own dispatcher.


Question: Where is the Dispatcher of the thread saved?

  1. In TLS ( thread local storage)
  2. In a  system wide Static List
  3. Dispather will not be saved inside thread
  4. In a system wide Static Dictionary

Answer: 4, In a system wide Static Dictionary


Question: If Dependency properties are static then how  individual control gets the different value?
Answer: 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.


Question: 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

Answer: Visual class


Question: Which design pattern from Gang of Four closely matches with Attached Properties in WPF and Why?
Answer: Its Decorator Design Pattern because Attached Properties are also adding behavior at run time to the objects


Question: Can you add click event to the grid?

  1. Yes, just create a custom control which mimics the functionality of GRID and BUTTON.
  2. No, its not possible
  3. Yes, By Attached Properties
  4. Yes, By Attached Events

Answer: Yes, Attached Events


Question: Relative Source Binding is possible due to the Visual Tree?

  1. Yes
  2. No, There is no relative source binding

Answer: 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.


Question: If you want to modify a control which is owned by secondary thread from UI Thread, how will you do it?

  1. By Using Current Thread’s Dispatcher, if no dispatcher is there, WPF runtime will create one
  2. By using main thread’s Dispatcher, if no dispatcher is there, WPF runtime will create one
  3. By Using Current Thread’s Dispatcher, if no dispatcher is there, programmer can create one
  4. You cannot do it, control created must be owned by UI thread

Answer: 4, Although you can create a UI control in separate thread but you need to access it


Question: Does Background Worker class bypass Dispatcher mechanism while updating a control owned by UI thread?
Answer: No because Background worker class internally call the Dispatcher to process input on UI.


Question: Which Feature of WPF mostly makes MVVM so compatible with various Design principle ( e.g. SOLID)?
Answer: Data binding as it eliminates the need of writing getter and setter code in “.xaml.cs”


Question: 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?

  1. Gird
  2. Dock panel
  3. Vistulizing Stack Panel
  4. Canvas

Answer: Canvas, since we know the position , so there is no need to calculate in Layout process.


Question: Can you handle the mouse move / drag and drop event in ViewModel if you are following MVVM design pattern if yes than how?
Answer: Yes, you need to use EventTrigger or Behavior for this which are found in System.Windows.Intractivity dll.


Question: In a  true strict MVVM implementation (where you wont find .xaml.cs file), can you use RoutedCommand ? If yes How , If No then Why?
Answer: 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.


Question: 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?
Answer: 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.

Tagged , . Bookmark the permalink.

Leave a Reply