是否有一个好的工具可以在运行时调试XAML的数据绑定行为/错误? [英] Is there a good tool for debugging XAML's databinding behavior / errors at runtime?
问题描述
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.
例如,我最近不得不更改 Style
声明,如下所示:
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>
到此:
<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>
为了影响DataGridRow属性。在设计或运行时查看绑定到不同源和 RelativeSource
的含义将是非常有用的。
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?
推荐答案
您可以使用 PresentationTraceSources.TraceLevel
绑定的附加属性,以在运行时在输出中获得详细的日志记录。
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>
这篇关于是否有一个好的工具可以在运行时调试XAML的数据绑定行为/错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!