WPF Interview Questions
This page links to interview questions for WPF, binding, and MVVM.
WPF Interview Questions – WPF
Level 1
- Q: What the first class that is loaded by any new WPF Application project?
A: App. App.xaml and App.xaml.cs. It is also acceptable if they say App. - Q: What is a DependencyProperty?
A: Represents a property that can be set through methods such as, styling, data binding, animation, and inheritance. [1] - Q: Name as many layout controls as you can:
A: Grid, DockPanel, Canvas, WrapPanel, UniformGrid, StackPanel - Q: What tool should you use sketch a mock of your WPF application?
A: SketchFlow
Level 2
- Q: Describe how you would go about writing your own button style?
A: Make sure the interviewee understands the following:
– How to copy the default style using Expression Blend.
– How to change the style
– How there must be a Style for each state: Enabled, Disabled, Mouseover, pushed, etc… - Q: You need to display a list of items. Which controls are possible options? Of those, which would you use and why?
A: ItemsControl, ListBox, ListView, DataGrid.
– Make sure they know that ListBox and ListView can be selected while ItemsControl does not support selection.
– ListView is more feature rich, as it inherits ListBox, which inherits ItemsControl, and adds features.
– Make sure they understand what a DataGrid is and how it is different and why they would display a list in a DataGrid. - Q: What can you do in Expression Blend that you cannot do in Visual Studio?
A: Configure brushes and gradient brushes in the Properties window.
A: Configure Visual States.
A: Configure sample data.
A: Configure Object Timelines
A: Animation
A: Many design features… - What can you do in Visual Studio that you cannot do in Expression Blend?
A: Reformat source code, including XAML, with a hot key.
A: Include Solution Folders.
Level 3
- What are common Localization/Globalization practices for Localizing/Globalizing WPF?
A: 1) Use Resources.resx, 2) Use BAML, 3) Use a ResourceDictionary to manage localizable strings. - Q: What are some best practices when using WPF if you plan to build a Localized/Globalized application?
A: Write UI in XAML
A: Avoid sizing and positionings, but let objects automatically flow.
A: Enable TextWrapping. - C# applications usually start with this method:
public static void Main(string[] args) { }
Q1: Where is this method in a WPF Application?
A: Under the project an autogenerated class file called App.g.i.cs is created as a partial class of the App class. So the Main function is part of App but found in App.g.i.cs.
Q2: If you needed to add a line into the Main function, how would you do it?
A: You add a new class, such as Program.cs, and in the project properties you set Program.cs as the Startup Object.
WPF Interview Questions – Binding
Level 1
- Q: How do you bind a bool value to Visibility?
A: Add a BoolToVisibilityConverter to your resources and then use it as the converter in your Binding. - Q: What is IValueConverter used for?
A: So that a control or control property can be bound to an element of any data type and the conversion happens in the binding process. - Q: Here is a TextBox. Change it so that it will bind to the ‘Text’ property of any DataContext.
<TextBox />
A:
<TextBox Text="{Binding Path=Text}" />
- Q: What happens if the value you are binding to does not exist?
A: The exception does NOT crash the program. If running from Visual Studio, a message is logged in the Output window.
Level 2
- You want to use a custom Binding but the control does not support binding in the area you want. What are your options? Which would you choose and why?
A1: Inherit object and add a Dependency Property – If the object is not sealed this is likely an easier option.
A2: Create an attached property – The object might be sealed. - Q: How do you bind a button’s Command property to a method?
A: You don’t. You bind to an ICommand. You create a class that implements ICommand and in that object you connect to your method. - Q: Provide an example of when and why you would use MultiBinding.
A: When you have multiple values that your which to combine.
A: When you want to use String.Format in XAML. - Q: What is the Binding syntax for binding to a Static value?
A:{x:Static s:MyStaticClass.StaticValue2}
Level 3
- Q: Provide a situation in which you have or would use a ConverterParameter.
A: Any time you have a converter that returns something different based on a parameter.
A: Look for them to share an example of needing to do this. - What is the correct syntax for binding to a property of a Singleton?
A:{Binding Source={x:Static sing:MySingletonClass.Instance}, Path=SomeProperty}
WPF Interview Questions – MVVM
Level 1
- Q: What is INotifyPropertyChanged used for?
A: For notifying any observer, usually a WPF control, that the property it is bound to has changed. - Q: What is the purpose of MVVM?
A: To provide a design pattern that allows for separation of concerns. The UI is independent of the data and the data is independent of the UI. - Do you prefer and MVVM framework or just a few MVVM objects?
A: No real right answer here, just checking how they actually have implemented MVVM. - Q: What object do most ViewModels inherit from:
A: Either ObservableObject or ViewModelBase. - Q: How do you implement binding a button click to a method?
A: Create a class that implements ICommand, often called RelayCommand or RoutedCommand.
Add an ICommand property to your ViewModel and instantiate the property using your ICommand implementation, RelayCommand or RoutedCommand.
Level 2
- What gotchas have you experience while using the MVVM pattern?
A: No real answer here…just that they have experienced problems proves their experience. Example issues:
– The ViewModel just becomes a place for all the code-behind.
– Determining whether the View can, should, or should not reference the ViewModel.
– Determining if View objects can, should, or should not be in the ViewModel.
– When breaking Views down, how small to go.
– When breaking ViewModels down, how small to go.
– Should an object in the model implement ObservableObject? - Q: Why is it better to use an IValueConverter instead of performing the conversion in the ViewModel?
A: This follows the Don’t Repeat Yourself (DRY) principle. It also follows the Single Responsibility Principle (SRP).
Because you may have to perform the same conversion between multiple View and ViewModel combinations. If each ViewModel has the code, the code is duplicated. If the code is in an IValueConverter, it exists in one place and is resusable.
Level 3
- Q: What does this command do and when would you use it? CommandManager.InvalidateRequerySuggested()
A: It causes the any commands, such as a binding to Button.Command to check again if it can execute.
It is used when the command runs and the button should be enabled when the command completes, but the button stays disabled. This often occurs when running the command on a thread or by using a BackgroundWorker. - Q: When should you use “code-behind” in MVVM.
Note: Don’t except ‘never’ as an answer.
A: When the code only involves the View or WPF Controls in the View.
A: When you need to implement events that do not support binding to ICommand.