DataBinding is one of the most powerful features in WPF. But because it resolves the bindings at runtime and does not throw exceptions, it's sometimes hard to find the reason why the data do not appear as expected. There are mainly two reasons:html
In the example, the text property of the TextBlock is bound to the property "InvalidPath" of the StackPanel - which does not exists. express
<Window x:Class="DebugDataBinding.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <StackPanel x:Name="stack"> <TextBlock Text="{Binding ElementName=stack, Path=InvalidPath}" /> </StackPanel> </Window>
In this case the invalid databinding expression is reported by a trace message in the output windowapp
System.Windows.Data Error: 39 : BindingExpression path error: 'InvalidPath' property not found on 'object' ''StackPanel' (Name='stack')'. BindingExpression:Path=InvalidPath; DataItem='StackPanel' (Name='stack'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
this
Note: Binding to a path of a property that has NULL value is a valid expression and does not generate an error message (for e.g. binding to a property of the data context that is NULL).spa
NET3.5 has a new feature that allows you to set the level of details of trace messages to None
, Low
, Medium
or High
..net
To set the trace level you have to include an extra namespace to your XAML:debug
<Window x:Class="DebugDataBinding.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"> <StackPanel x:Name="stack"> <TextBlock Text="{Binding ElementName=stack, Path=InvalidPath, diag:PresentationTraceSources.TraceLevel=High}" /> </StackPanel> </Window>
The following snipped shows how to adjust the trace level by code:code
PresentationTraceSources.DataBindingSource.Listeners.Add( new ConsoleTraceListener()); PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.All;
A simple trick is to write a value converter that does nothins except breaking into the debugger. All you need to do now is to add this converter to the binding expression that fails and you can easily see the values that should be bound.xml
/// <summary> /// This converter does nothing except breaking the /// debugger into the convert method /// </summary> public class DatabindingDebugConverter : 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; } }
To use the converter in XAML, reference the namespace of the assembly that contains the converter and add an instance of it to the resources of your window.htm
<Window x:Class="DebugDataBinding.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DebugDataBinding" Title="Window1" Height="300" Width="300"> <Window.Resources> <local:DatabindingDebugConverter x:Key="debugConverter" /> </Window.Resources> <StackPanel x:Name="stack"> <TextBlock Text="{Binding ElementName=stack, Path=ActualWidth, Converter={StaticResource debugConverter}}" /> </StackPanel> </Window>