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 RelativeSource
s would be.
Do any such tools / techniques exist?
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.
To enable this experience, go to “Options > Environment > Preview Features”, “XAML Binding Failure Window” and restart Visual Studio.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With