C# Disciples

my life in Avalon ….

Debugging WPF DataBinding

How much do we use DataBinding in WPF?
I would say a lot… I guess DataBinding is one of the biggest features of this platform.Sometimes we need to debug the values that are passed in the binding yet since the data binding is created by the XAML parser we do not have any way to create a VS breakpoint and step into the code….

One way for debugging data binding is to use a converter and set a breakpoint in the Convert method. This can be cumbersome to work with since you have to create a dummy converter and set breakpoints inside it. It would be nice if there is a way how one can just enter a flag in the Binding and the debugger would break to let you check out the Binding values. This can be achived by using markup extentions. Markup extentions are basically a way how one can create instances of specific objects from XAML. For example {Binding PropertyX} is a markup extension to create a binding object. So what we can do is basically create a Markup extention that returns a Converter that we can use to debug the Binding. The converter calls the Break method of the Debugger class to force a breakpoint in the Convert method.

The Markup Extention for our converter would look something like this…

/// <summary>
/// Converter to debug the binding values
/// </summary>
public class DebugConvertor : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Debugger.Break();
return Binding.DoNothing;
}
…..#endregion}/// <summary>
/// Markup extension to debug databinding
/// </summary>
public class DebugBindingExtension : MarkupExtension
{/// <summary>
/// Creates a new instance of the Convertor for debugging
/// </summary>
/// <param name=”serviceProvider”></param>
/// <returns>Return a convertor that can be debugged to see the values for the binding</returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new DebugConvertor();
}
}

Basically the most important part is the ProvideValue method of the Markup extention. Here we create an instance of the converter and return it. Now what we need to do, is to make the binding use this markup extension in order to use the DebugConverter (that will automatically break the debugger so that you can debug your binding). The following XAML shows how easy it is to use the extension

<Window x:Class=”DebuggingDataBinding.Window1″
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local=”clr-namespace:DebuggingDataBinding”
Title=”Window1″ Height=”300″ Width=”300″>

<
Grid Height=”{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Height,
Converter={local:DebugBinding}}”>
………
</Grid>
</
Window>

Basically the Converter={local:DebugBinding} does the whole thing. It will create an instance of the converter and return it to the binding converter property. So there you have it a simple yet handy Markup Extention to debug data binding….

P.S I created a Demo application that includes all source code for any one interested
Download Here

December 18, 2007 - Posted by marlongrech | .Net 3.0, WPF | | 10 Comments

10 Comments »

  1. Nice!

    Comment by Josh Smith | December 18, 2007 | Reply

  2. Good example. I learned about DoNothing too.

    Thank you.

    Comment by Karl Shifflett | December 19, 2007 | Reply

  3. [...] Marlon Grech wrote a Markup Extension that you can use in your DataBinding that will auto magically break in Visual studio so that you can see your binding value. Your markup [...]

    Pingback by Debugging in WPF - WPF Disciples tips and tricks « C# Disciples | April 4, 2008 | Reply

  4. [...] Marlon Grech wrote a Markup Extension that you can use in your DataBinding that will auto magically break in Visual studio so that you can see your binding value. Your markup [...]

    Pingback by Debugging in WPF - WPF Disciples tips and tricks « WPF Disciples | April 4, 2008 | Reply

  5. You sample source code seems to have gone missing ? =)

    Comment by Nicolai Sørensen | June 12, 2008 | Reply

  6. thanks for pointing it out…. here is the download
    http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/DebuggingDataBinding.zip

    Comment by marlongrech | June 12, 2008 | Reply

  7. And what do you do if the Debugger doesn’t break?! :)

    I’m trying to debug a rather unusual, swiss-army knife use for an IValueConverter that passes in the Converter resource and a ConverterParameter. My use case is to use the ConverterParameter as a “primary/foreign key” filter on a bound Collection object, and to return a display of the only object in the collection that can possibly match the key.

    Comment by John "Z-Bo" Zabroski | July 17, 2008 | Reply

  8. I find debugging triggers generally more painful than debugging bindings. There’s a good post here on how they can be traced using WPF tracing:

    http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html

    Comment by WPF Mentor | January 23, 2009 | Reply

  9. [...] you can find a nice implementation of this in this post [...]

    Pingback by DataBinding Tips & Tricks « Elad’s WPF Blog | April 8, 2009 | Reply

  10. Pretty nice.

    Comment by xxx | July 10, 2009 | Reply


Leave a comment