Posted by: Zeeshan Amjad | November 9, 2014

Debug data binding


Sometimes debugging data binding is very time consuming. There can be several reason for data binding to not work. I came across one situation where the property is private.

Here is a small tip to debug the data binding. We can use Debugger.Break() method to put the break point and uses the converter to do this. Here is a small utility class to do this.

Code Snippet
namespace DebugBinding
{
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.Windows.Data;
    using System.Windows.Markup;

    [ValueConversion(typeof(object), typeof(object))]
    public class DebugDataBindingExtension : MarkupExtension, IValueConverter
    {
        #region IValueConverter

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

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

        #endregion

        #region MarkupExtension

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        #endregion
    }
}

 

And here is a XAML to use this markup extension.

Code Snippet
<ListBox Margin="5" ItemsSource="{Binding Converter={local:DebugDataBinding}}"/>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: