Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good tool for debugging XAML's databinding behavior / errors at runtime?

Tags:

.net

wpf

xaml

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?

like image 942
Dan J Avatar asked Oct 26 '10 18:10

Dan J


People also ask

How do I debug XAML code in Visual Studio?

To debug workflow XAML Open a workflow or activity project in Visual Studio. Set a breakpoint on the activity or activities you want to debug as described in How to: Set Breakpoints in Workflows. Right-click the . xaml file that contains your workflow definition and select View Code.

How fix XAML binding failures?

To enable this experience, go to “Options > Environment > Preview Features”, “XAML Binding Failure Window” and restart Visual Studio.


2 Answers

You can use PresentationTraceSources.TraceLevel attached property on bindings to get detailed logging in the output during 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> 
like image 76
Athari Avatar answered Sep 21 '22 00:09

Athari


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.

like image 25
John Myczek Avatar answered Sep 21 '22 00:09

John Myczek