*
Previous Code Coverage and Static Analysis in C# WPF-Question-Answers-71-80 Next

WPF (Windows Presentation Foundation) Question Answers - (61–70)

WPF Interview Questions (61 - 70)

61. What is Invoke method in WPF?

Invoke method takes an Action or Delegate and executes the method synchronously. That means it does not return until the Dispatcher completes the execution of the method.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Task.Factory.StartNew(() =>
        {
            InvokeMethodExample();
        });
    } 
    private void InvokeMethodExample()
    {
        Thread.Sleep(2000);
        Dispatcher.Invoke(() =>
        {
            btn1.Content = "By Invoke";
        });
    }
}
    

This code creates a new thread using Task.Factory and updates the button content via Dispatcher.Invoke to avoid System.InvalidOperationException.

62. What is Logical tree and Visual tree in WPF?

Visual Tree: Represents all elements in the UI that render to an output device (e.g., Border, AdornerDecorator, ContentPresenter). Event routes for routed events mostly travel along the visual tree.

Logical Tree: Represents the essential structure of the UI (e.g., Window, Grid, Button, Label). Used for resource lookup and notifications when elements are loaded.

63. What is PropertyChangedCallback?

Represents the callback invoked when the effective property value of a dependency property changes. Often used to update internal state when a public property changes.

64. What is relative resource?

Describes the location of the binding source relative to the binding target. Valid values:

  • FindAncestor – Bind to an ancestor of a specific type.
  • PreviousData – Bind to the previous data item in a list.
  • Self – Bind one property of an element to another property on the same element.
  • TemplatedParent – Bind to the element a template is applied to.
{Binding RelativeSource={RelativeSource Self}}
{Binding RelativeSource={RelativeSource Self}, Path=Name}
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Title}
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=2}, Path=Name}
{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name}
{TemplateBinding Name}
    

65. What is RelayCommand in WPF?

In MVVM Light Toolkit, RelayCommand implements ICommand. It requires an Execute delegate and optionally a CanExecute delegate returning a Boolean.

66. What is routed event in WPF?

A routed event can invoke handlers on multiple listeners in an element tree, not just the object that raised the event. Backed by RoutedEvent and processed by the WPF event system.

67. What is target and source in WPF binding?

In data binding, the source contains the data, and the target is the control displaying that data.

68. Benefit of using a markup language for UI design

Markup languages like XAML or HTML provide a common medium for designers and developers, and allow a declarative approach to building applications.

69. Difference between Panel and Decorator

A Panel arranges multiple child elements; a Decorator has only one child and applies additional behavior to it.

70. Difference between CoerceValueCallback and PropertyChangedCallback

PropertyChangedCallback executes only when the property value changes. CoerceValueCallback executes every time a value is assigned, even if it’s the same as the old value.

Back to Index
Previous Code Coverage and Static Analysis in C# WPF-Question-Answers-71-80 Next
*
*