C# / .NET Compendium PLEN
.NET / C# / WPF

WPF - what is worth refreshing before an SE III interview

Besides MVVM/DI, in WPF interviews (especially at level III) they like to check your understanding of the mechanisms "under the hood", not just the ability to click around in XAML. Here is what I would revise, grouped by theme.

1. Dependency Properties - the foundation of WPF

This is probably the most frequently asked topic apart from MVVM itself - the difference between an ordinary C# property and a Dependency Property.

public class RatingControl : Control
{
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register(
            nameof(Value),
            typeof(int),
            typeof(RatingControl),
            new FrameworkPropertyMetadata(
                defaultValue: 0,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                OnValueChanged));

    public int Value
    {
        get => (int)GetValue(ValueProperty);
        set => SetValue(ValueProperty, value);
    }

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // reaction to a value change
    }
}

Why DPs exist instead of a plain { get; set; }: - Built-in support for data binding (without a manual INotifyPropertyChanged) - Property value inheritance - values are inherited down the visual tree (e.g. FontSize, DataContext) - Support for styles, animations, triggers - all of this works only on DPs - Automatic memory reduction - a DP takes no per-instance memory if it has the default value (stored in a static dictionary)

Attached Property - similar, but "attached" to any element, not just the owner:

// Grid.Row, Grid.Column to klasyczne attached properties
<Button Grid.Row="0" Grid.Column="1" />

2. Routed Events - bubbling vs tunneling

A common question: "What is the difference between PreviewMouseDown and MouseDown?"

Tunneling (Preview...): from the root of the tree DOWN to the source element
Bubbling (no Preview): from the source element UP to the root
<StackPanel MouseDown="Parent_MouseDown">
    <Button Content="Kliknij" MouseDown="Button_MouseDown" />
</StackPanel>

Clicking a button: first PreviewMouseDown travels from the window down to the button (tunneling), then MouseDown from the button up to the window (bubbling). You can stop the propagation with e.Handled = true.

3. Data binding - details that are easy to forget

<!-- Mode: OneWay, TwoWay, OneTime, OneWayToSource, Default -->
<TextBox Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<!-- RelativeSource - binding to a property of an ancestor in the tree -->
<TextBlock Text="{Binding DataContext.Title, RelativeSource={RelativeSource AncestorType=Window}}" />

<!-- ElementName - binding do innego elementu na tej samej stronie -->
<Slider x:Name="VolumeSlider" Maximum="100" />
<TextBlock Text="{Binding Value, ElementName=VolumeSlider}" />

IValueConverter - converting data between Model and View:

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => (bool)value ? Visibility.Visible : Visibility.Collapsed;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => throw new NotImplementedException();
}

ObservableCollection - why a plain List<T> is not enough when binding to an ItemsControl:

// Wrong - changing the contents will NOT notify the UI
public List<Order> Orders { get; set; } = new();

// Dobrze - implementuje INotifyCollectionChanged
public ObservableCollection<Order> Orders { get; set; } = new();

4. Visual Tree vs Logical Tree

A common conceptual question: - Logical Tree - the structure of elements defined in XAML (what you write) - Visual Tree - the full structure of rendered elements, including those generated by templates (e.g. the internal parts of a Button - Border, ContentPresenter)

// Searching the visual tree - a typical "find the parent/child of a given type" task
public static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(child);
    if (parent == null) return null;
    return parent as T ?? FindVisualParent<T>(parent);
}

5. Threading - Dispatcher and the UI thread

This is a very common topic in interviews - WPF has a single-threaded UI model (so-called STA - Single Threaded Apartment).

// Error - the exception "The calling thread cannot access this object"
private async Task LoadDataAsync()
{
    var data = await Task.Run(() => _repository.GetOrders()); // background thread
    OrdersList.ItemsSource = data; // ❌ attempt to access the UI from another thread
}

// Dobrze - Dispatcher.Invoke wraca na UI thread
private async Task LoadDataAsync()
{
    var data = await Task.Run(() => _repository.GetOrders());
    Application.Current.Dispatcher.Invoke(() =>
    {
        OrdersList.ItemsSource = data;
    });
}

// Jeszcze lepiej - async/await z SynchronizationContext WPF
// automatically returns to the UI thread after await, unless you use ConfigureAwait(false)
private async Task LoadDataAsync()
{
    var data = await _repository.GetOrdersAsync(); // wraca na UI thread automatycznie
    OrdersList.ItemsSource = data; // ✅ OK
}

It is worth being able to explain why - await by default captures the SynchronizationContext and returns to the same thread (the UI thread), unless you use ConfigureAwait(false) (which in ViewModel code you usually do not want to do, because then you have to return to the UI thread manually).

6. Styles, Templates, Triggers

<Style TargetType="Button" x:Key="PrimaryButtonStyle">
    <Setter Property="Background" Value="DodgerBlue" />
    <Style.Triggers>
        <!-- DataTrigger - reaction to a value from a binding -->
        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="False">
            <Setter Property="Opacity" Value="0.5" />
        </DataTrigger>
        <!-- Trigger - reaction to a property of the element itself -->
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="RoyalBlue" />
        </Trigger>
    </Style.Triggers>
</Style>

ControlTemplate vs DataTemplate - a difference worth being able to explain: - ControlTemplate - defines how a control looks (e.g. what a Button looks like on the inside - Border, ContentPresenter) - DataTemplate - defines how to display data (e.g. what a single item in a ListBox bound to an Order object should look like)

7. Performance - the typical question "what would you optimise"

  • UI Virtualization in ItemsControl/ListBox - so as not to render thousands of elements at once (VirtualizingStackPanel.IsVirtualizing="True", enabled by default in newer versions)
  • Freezable objects (e.g. Brush, Geometry) - calling .Freeze() improves performance, because WPF doesn't have to listen for changes
  • Avoiding overly deep nesting of Binding with a Converter doing heavy calculations (a binding can fire very often)

8. Things specific to newer versions (.NET 6/8 WPF)

Since they will probably ask about the current stack: - CommunityToolkit.Mvvm with source generators - [ObservableProperty], [RelayCommand] instead of manual SetProperty/RelayCommand - WPF on .NET Core/.NET 5+ (not .NET Framework) - differences in hosting, Microsoft.NET.Sdk

// Nowoczesny styl z CommunityToolkit.Mvvm
public partial class UserProfileViewModel : ObservableObject
{
    [ObservableProperty]
    private User _user;

    [RelayCommand]
    private async Task LoadUserAsync()
    {
        User = await _userService.GetUserAsync("123");
    }
}

The generator itself creates a property with SetProperty, PropertyChanged and LoadUserCommand as an ICommand - much less boilerplate than what we wrote by hand earlier.


Given your C# experience, most of this is probably "in your muscle memory" - the most likely SE III questions are the more conceptual ones (DP vs an ordinary property, why the Dispatcher, bubbling vs tunneling, Visual vs Logical Tree), because they test understanding of why WPF works the way it does, not the syntax itself.

Buy me a coffee