Skip to main content

Debugging databinding

WPF and Silverlight are great thanks to the rich databinding support. Important to notice is that late bound data binding is used bindings in XAML files. This feature allows a DataContext to be set at run-time and the objects within that DataContext to resolve their property bindings then. This late binding enables cool features like DataTemplates, composable applications and run-time loading of loose XAML.

A side effect of late bound binding resolution that databinding will fail silently if a value can not be found.  So troubleshooting why a specific field is not loaded in a control can be painfull. Therefore some tips to get some useful debugging information.

View the Debug Output Window

If you have the DataContext set, any data bindings within that DataContext that can't be resolved will be listed in the Debug Output Window at run-time.

WPF Trace Settings, Data Binding

The default value of the WPF Trace Settings, Data Binding is "Error." To get more data binding information change the level to "Warning". If the value is set to "Error" you won't see any of the PresentationTraceSources.TraceLevel warning messages.

To change the WPF Trace Settings, Data Binding value to "Warning," navigate to Tools, Options, Debugging, Output Window, WPF Trace Settings, Data Binding and set the value to Warning.

PresentationTraceSources.TraceLevel

Introduced to WPF in .NET 3.5 SP1, the PresentationTraceSources.TraceLevel attached property allows developers to dial up the amount of information outputted in the Debugger Output Window for a binding.

The below XAML snippet shows how to apply the PresentationTraceSources.TraceLevel attached property to a binding. 

   1:  <Window 
   2:      xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
   3:      xmlns:local="clr-namespace:DebugDataBindings"
   4:      x:Class="DebugDataBindings.MainWindow"
   5:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   6:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   7:      Title="MainWindow" Height="350" Width="525">
   8:      <Grid x:Name="layoutRoot">
   9:          <Grid.Resources>
  10:              <local:DebugConverter x:Key="debugConverter" /> 
  11:          </Grid.Resources>
  12:          <TextBox 
  13:              Text="{Binding Path=Customer.FirstName, diag:PresentationTraceSources.TraceLevel=High}"   
  14:              Height="23" HorizontalAlignment="Left" Margin="90,18,0,0" VerticalAlignment="Top" Width="120"/>
  15:      </Grid>
  16:  </Window>

Still not enough information? Have a look at the following blog posts:


Popular posts from this blog

.NET 8–Keyed/Named Services

A feature that a lot of IoC container libraries support but that was missing in the default DI container provided by Microsoft is the support for Keyed or Named Services. This feature allows you to register the same type multiple times using different names, allowing you to resolve a specific instance based on the circumstances. Although there is some controversy if supporting this feature is a good idea or not, it certainly can be handy. To support this feature a new interface IKeyedServiceProvider got introduced in .NET 8 providing 2 new methods on our ServiceProvider instance: object? GetKeyedService(Type serviceType, object? serviceKey); object GetRequiredKeyedService(Type serviceType, object? serviceKey); To use it, we need to register our service using one of the new extension methods: Resolving the service can be done either through the FromKeyedServices attribute: or by injecting the IKeyedServiceProvider interface and calling the GetRequiredKeyedServic...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...