tags:

views:

45

answers:

3

WPF is a great toolset, and XAML databinding is very powerful, but I've often run into difficulty arising from its transparency: It can be tough to debug a databinding failure when no errors are thrown.

For example, I recently had to change a Style declaration like this:

<DataGrid.RowStyle>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding TestProperty}" Value="False">
                <Setter Property="DataGridRow.Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

Into this:

<DataGrid.RowStyle>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TestProperty}" Value="False">
                <Setter Property="DataGridRow.Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

In order for the DataGridRow property to be affected. It would be incredibly helpful to see, at design- or run-time, what the implications of binding to different sources and RelativeSources would be.

Do any such tools / techniques exist?

+1  A: 

If you run your application under the Visual Studio debugger, binding errors will be reported to the output window. These are very informative and should help you track down any errors.

JaredPar
+3  A: 

You can use PresentationTraceSources.TraceLevel attached property on bindings to get detailed logging in the output during runtime. See http://msdn.microsoft.com/en-us/library/system.diagnostics.presentationtracesources.tracelevel.aspx

In your case, it will look like this:

<DataGrid.RowStyle>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TestProperty, PresentationTraceSources.TraceLevel=High}" Value="False">
                <Setter Property="DataGridRow.Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>
Athari
This definitely helped - accepted! For completeness, since inspecting the visual tree at runtime is a related concern, I'd also like to point out [Snoop](http://www.blois.us/Snoop/) to anyone who remains unaware of its charms.
djacobson
The most recent version of Snoop can be found on CodePlex: http://snoopwpf.codeplex.com/
John Myczek
+4  A: 

Bea Stollnitz has a very informative blog post about debugging WPF bindings.

If you are using Visual Studio 2010, you will need to update the default WPF trace setting.

John Myczek
+1 Thanks very much for pointing out the need to update the VS2010 setting; I would've burned a lot of time on that one.
djacobson