Is there a good tool for debugging XAML binding behavior / errors at runtime?

WPF is a great toolkit and XAML data binding is very efficient, but I often run into difficulties with its transparency: it can be difficult to debug data binding failure if errors are not thrown.

For example, I recently had to modify a Style declaration as follows:

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

In it:

 <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> 

To affect the DataGridRow property. It would be incredibly useful to see, during design or runtime, what consequences might be associated with different sources and RelativeSource .

Are there any such tools / methods?

+28
wpf xaml
source share
4 answers

You can use the PresentationTraceSources.TraceLevel attached property on bindings to get detailed output logging at runtime.

In your case, it will look like this:

 <DataGrid.RowStyle> <Style> <Style.Triggers> <DataTrigger Value="False" Binding="{Binding DataContext.TestProperty, RelativeSource={RelativeSource AncestorType=UserControl}, PresentationTraceSources.TraceLevel=High}"> <Setter Property="DataGridRow.Background" Value="Red"/> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowStyle> 
+30
source share

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

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

+4
source share

If you run the application under the Visual Studio debugger, binding errors will be sent to the output window. They are very informative and should help you identify any errors.

+3
source share

Another option in Visual Studio 2010 is to set a breakpoint on the anchor. This can be easily done by placing your click on the Binding keyword and pressing F9 to set a breakpoint. The Binding keyword will be highlighted in red. If a large piece of text is selected, it may be that the breakpoint does not work in the editor you are using. In this case, try right-clicking the file in Solution Explorer, select "Open With" and select "Source Code Editor (text)."

When you hit a breakpoint at runtime, you can test it using the debug locator window and see what it is attached to.

0
source share

All Articles