This is a mirror of official site: http://jasper-net.blogspot.com/

Debugging WPF data bindings

| Tuesday, August 23, 2011
Introduction

Data bindings is one of the most widely used features of WPF. However, debugging data bindings is not as well known, which I'll attempt to rectify with this article.

There are essentially two methods we can use to debug our data bindings, when they do not throw an exception.

    Using IValueConverter to step into the debugger.
    Using the trace messages.

Using IValueConverter

This method is pretty straightforward. You'll create a class implementing the IValueConverter interface and then use this converter in your binding so you can step into the debugger.

// Either you can use Debugger.Break() or put a break point
// in your converter, inorder to step into the debugger.
// Personally I like Debugger.Break() as I often use
// the "Delete All Breakpoints" function in Visual Studio.
public class MyDebugConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
           object parameter, CultureInfo culture)
    {
        System.Diagnostics.Debugger.Break();
        return value;
    }

    public object ConvertBack(object value, Type targetType,
           object parameter, CultureInfo culture)
    {
        System.Diagnostics.Debugger.Break();
        return value;
    }
}

<TextBox Text="{Binding MyProperty, Converter={StaticResource MyDebugConverter}}"/>

Now whenever your binding provides a new value, the debugger will break at Debugger.Break() in your converter and you can step out to see which property is set and where it was set.

Read more: Codeproject
QR: DebuggingWPFDataBindings.aspx

Posted via email from Jasper-net

0 comments: