User Tools

Site Tools


technical_notes:public:20131007-wpf-how-to-debug-xaml

Parent

WPF - How to debug Xaml binding ?

There isn't tool to debug the binding naturally as we can do for C# code. It exists a mechanism to trace the binding and it's result.

Solution 1 : Trace

PresentationTraceSources

To use it, you have to declare in your xaml its namespace :

<!-- Add diag namespace -->
<wpf:ChildWindow xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase">
  ...
  ...
  ...
</wpf:ChildWindow>
<!-- add the tracert -->
<ToolTipService.ToolTip>
  <ToolTip DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext, diag:PresentationTraceSources.TraceLevel=High}" />
</ToolTipService.ToolTip>

When you request the binding in the output window of Visual studio (if you use it), you will get those informations.

System.Windows.Data Warning: 56 : Created BindingExpression (hash=48861387) for Binding (hash=41459726)
System.Windows.Data Warning: 58 :   Path: 'DataContext'
System.Windows.Data Warning: 60 : BindingExpression (hash=48861387): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=48861387): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=48861387): Attach to System.Windows.Controls.ToolTip.DataContext (hash=37099307)
System.Windows.Data Warning: 67 : BindingExpression (hash=48861387): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=48861387): Found data context element: <null> (OK)
System.Windows.Data Warning: 72 :   RelativeSource.TemplatedParent found Control (hash=65458312)
System.Windows.Data Warning: 78 : BindingExpression (hash=48861387): Activate with root item Control (hash=65458312)
System.Windows.Data Warning: 108 : BindingExpression (hash=48861387):   At level 0 - for Control.DataContext found accessor DependencyProperty(DataContext)
System.Windows.Data Warning: 104 : BindingExpression (hash=48861387): Replace item at level 0 with Control (hash=65458312), using accessor DependencyProperty(DataContext)
System.Windows.Data Warning: 101 : BindingExpression (hash=48861387): GetValue at level 0 from Control (hash=65458312) using DependencyProperty(DataContext): ReadOnlyObservableCollection`1 (hash=52253899 Count=1)
System.Windows.Data Warning: 80 : BindingExpression (hash=48861387): TransferValue - got raw value ReadOnlyObservableCollection`1 (hash=52253899 Count=1)
System.Windows.Data Warning: 89 : BindingExpression (hash=48861387): TransferValue - using final value ReadOnlyObservableCollection`1 (hash=52253899 Count=1)

Solution 2 : Debug Converter

To use a break point, you can use a debug converter.

class DebugConverter : IValueConverter
{
        object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
                Debugger.Break();
                return value;
        }
 
        object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
                Debugger.Break();
                return value;
        }
}

You can use it in xaml

<Window xmlns:converter="clr-namespace:Tuto.Converter">
        <Window.Resources>
            <converter:DebugConverter x:Key="DebugConverter" />
        </Window.Resources>
    <Grid Margin="10">
        <TextBlock Text="{Binding name, ElementName=pers, Converter={StaticResource DebugConverter}}" />
    </Grid>
</Window>
, ,
technical_notes/public/20131007-wpf-how-to-debug-xaml.txt · Last modified: 2013/12/02 17:06 by Fabien Arcellier