Click here to Skip to main content
6,822,613 members and growing! (19,907 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Data Binding     Intermediate License: The Code Project Open License (CPOL)

Debugging WPF Databinding

By marlongrech

How to debug WPF Databinding
C# (C#1.0, C#2.0, C#3.0), .NET (.NET3.0, .NET3.5), XAML, Dev, Design
Posted:19 Dec 2007
Updated:3 Jan 2008
Views:10,189
Bookmarked:19 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 3.11 Rating: 4.00 out of 5

1

2
1 vote, 16.7%
3
3 votes, 50.0%
4
2 votes, 33.3%
5

Introduction

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 Visual Studio breakpoint and step into the code…

Implementing the Solution

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 in which 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 achieved by using markup extensions. Markup extensions are basically a way in which 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 extension 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 extension for our converter would look something like this…

debugbinding.jpg

Basically the most important part is the ProvideValue method of the markup extension. 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 extension to debug data binding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

marlongrech


Member
Check out my Blog
http://marlongrech.wordpress.com/

Currently building a WPF Controls library aka AvalonControlsLibrary
http://marlongrech.wordpress.com/avalon-controls-library/

Occupation: Other
Company: Uniblue Systems (www.uniblue.com)
Location: Malta Malta

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 3 Jan 2008
Editor: Deeksha Shenoy
Copyright 2007 by marlongrech
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project