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

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

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Color B