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

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

Help! I accidently enabled HSTS–on localhost

I ran into an issue after accidently enabling HSTS for a website on localhost. This was not an issue for the original website that was running in IIS and had a certificate configured. But when I tried to run an Angular app a little bit later on http://localhost:4200 the browser redirected me immediately to https://localhost . Whoops! That was not what I wanted in this case. To fix it, you need to go the network settings of your browser, there are available at: chrome://net-internals/#hsts edge://net-internals/#hsts brave://net-internals/#hsts Enter ‘localhost’ in the domain textbox under the Delete domain security policies section and hit Delete . That should do the trick…

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.