While playing with WPF, I’ve realized databindings are hard to debug, but the framework provides an easy way to be able to debug it. Let’s see:
What I’m Going to do is Create an UserControl with a CheckBox. This checkbox will be visible depending on the value of a Boolean Property of the User Control Named “ShowVisibleCheck”
First of all we create that property in the User Control:
private bool _ShowVisibleCheck = false;
public bool ShowVisibleCheck
{
get
{
return _ShowVisibleCheck;
}
set
{
_ShowVisibleCheck = value;
}
}
I could have used the functionality framework 3 gives to just declare the property
public bool ShowVisibleCheck {get;set;}
So why didn’t I do that?
To be able to bind a property, the property must:
1 – Be a dependency property (Unless you only bind by “OneWayToSource” when that is not required)
2- Implement the
INotifyPropertyChanged Interface (that is why I didn’t declare the property like that)
So let’s go:
To set this property as a dependency property we just register the dependency property as static, with the same name as our property and finishing the name with the “property” suffix. (note Row is the name of the class where my user control belongs to)
public static DependencyProperty ShowVisibleCheckProperty =
DependencyProperty.Register("ShowVisibleCheck", typeof(bool), typeof(Row));
Then we will implement the INotifyPropertyChanged Interface
In the class we set the interface:
public partial class Row : UserControl, INotifyPropertyChanged
This will set the members (in this case one event)
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
And in our property, we will notify when this property changes, so WPF will be able to redraw the binding according to the property.
Now, we have the property ok to be used, but there is one more problem. In this case I used a Boolean property instead a Visibility one to set the visibility of my CheckBox, I only have done this like this to introduce a converter into this code.
If I try to do a binding directly from a Boolean property (ShowVisibleCheck) versus a visibility one (Checkbox.Visible) I will have a casting problem. To help me solve that, I can use a class that will implement the IValueConverter that will manage to convert the value “true” to Visibility.Visible; and the value “false” to Visibility.Collapsed;
This is the code of my converter class:
namespace ReportDesigner.Classes
{
internal class VisibleConverter:IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Boolean source = (Boolean)value;
if (source)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
Note I receive an object. I know this object is a Boolean because the object I’ll be passing is the ShowVisibleCheck property, and the only thing I have to do is convert it to Visibility.
Once this is done, to specify to use the converter, we will use Converter={StaticResource visibleConverter} in the binding of the property, where visibleConverter is an static resource in my user control

And classes Represents the namespace of my converter:
xmlns:classes="clr-namespace:ReportDesigner.Classes"
So now everything should be placed in order to make the binding.
To bind to a property of myself we will use the keyword RelativeSource={RelativeSource Self} this means I’ll do the binding to another property of my class. If I put it on the checkbox, it means it will bind this property to another property in my checkbox.
So if the checkbox is contained in the user control, I can access It via the Path using the keyword Parent.
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Parent.ShowVisibleCheck, Converter={StaticResource visibleConverter}}
And Now it should work, but when I run it … It doesn’t work!. Why?
NOW IS WHEN THE DATABINDING DEBUGGING COMES!!!To introduce debugging of databinding there are two steps:
1 – Create a reference to System.Diagnostics in your namespaces
xmlns:debug="clr-namespace:System.Diagnostics;assembly=WindowsBase"2 –
Add debug:PresentationTraceSources.TraceLevel=High in the binding.
By doing this, Visual Studio will generate output information for the bindings, so I can check what is happening.
If I go to the output window I can see… Oh!, I forgot my control was contained in a Grid, and then in a StackPanel. I have to put another 2 Parents more in the Path!!!!
(In my case) UserControl (contains the property)->Grid->StackPanel->CheckBox

“System.Windows.Data Error: 39 : BindingExpression path error: 'ShowVisibleCheck' property not found on 'object' ''StackPanel' (Name='')'. BindingExpression:Path=Parent.ShowVisibleCheck; DataItem='CheckBox' (Name=''); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')”
So now I fix it, and this is how the binding ends:

Now it works!!!