Debugging WPF bindings

WPF and bindings can cause a lot of stress when your data binding doesn’t seems to work. From .Net 3.5 and up, you can enable trace information regarding bindings.

Add the following namespace in the header of your component:

xmlns:diag=”clr-namespace:System.Diagnostics;assembly=WindowsBase”

To your binding, add the following line:
diag:PresentationTraceSources.TraceLevel=High

For example:

<Window x:Class="DatabindingExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
 Title="MyWindow" Height="300" Width="300">
 <Grid>
 <TextBox Text="{Binding Path=Name, diag:PresentationTraceSources.TraceLevel=High}" />
 </Grid>
 </Window>
Also, to view the tracing, it is important to change the log level of Visual Studio regarding WPF. Go to Tools->Options, a dialog will appear. Click on the left on “Debugging->Output window”. You will see within the section “WPF Trace Settings” a line for binding. Set the loglevel for this on at least “Warning”.
Posted in C#, WPF by admin at June 14th, 2012.

Leave a Reply