Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...