Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Debugging Trick in XAML DataBinding

0.00/5 (No votes)
7 Jan 2011 1  
Debugging XAML DataBinding
It is difficult to Debug the XAML data binding in WPF.

As a developer, we always tend to debug our programs when there are problems.

If something goes wrong like:
- No Data display
- Showing some wrong data

You need to debug and see:
- Whether the binding is successful
- Does data object have values
- and so on...

Here is the Trick to Debug the Data Binding of XAML.

All you need to do is create a Converter class.

Example:
C#
public class DebugConverter : IValueConverter
{
        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
        #endregion
}


While binding the Data to the Control, use the DebugConverter as Converter.

Like, declaring the Converter in XAML:

XML
<Window.Resources>
<conv:DebugConverter x:Key="debugConverter" />
</Window.Resources>


Assigning to the Control whether we need to Debug the DataBinding:

XML
<ListBox ItemsSource="{Binding ElementName=windowObject, Path=CollectionOfString, Converter={StaticResource debugConverter}}"/>


Now place a Breakpoint at the Convert function. That's it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here