Debug DataBinding

Debug Data Bindings Using an IValueConverter

Problem

You need to debug a binding that is not working as expected and want to make sure the correct values are going in.

Solution

Create a converter class that implements System.Windows.Data.IValueConverter  and returns the value it receives for conversion, setting a breakpoint or tracepoint within the converter.

How It Works

Debugging a data binding can sometimes be quite tricky and consume a lot of time. Because data bindings are generally defined in XAML, you don't have anywhere you can set a breakpoint to make sure things are working as you intended. In some cases, you will be able to place a breakpoint on a property of the object that is being bound, but that option isn't always available, such as when binding to a property of some other control in your application. This is where a converter comes in.

When using a simple converter that returns the argument being passed in, unchanged, you immediately have some code that you can place a breakpoint on or write some debugging information to the Output window or some log. This can tell you whether the value coming in is the wrong type, in a form that means it is not valid for the binding, or is coming in with a strange value. You'll also soon realize whether the binding is not being used, because the converter will never be hit.

Debug Bindings Using Attached Properties

Problem

You need to debug a binding that is not working as expected and want to make sure the correct values are going in. Using a converter is either undesired or not feasible.

Solution

Use the System.Diagnostics.PresentationTraceSources.TraceLevel attached property defined in the WindowsBase assembly, setting the level of detail required. If the data binding is defined in code, use the static method PresentationTraceLevel.SetTraceLevel.

Caution

Using the PresentationTraceSources.TraceLevel attached property can affect the performance of a WPF application and should be removed as soon as it is no longer required.

 How It Works

The PresentationTraceSources.TraceLevel attached property allows you to specify the level of information written to the Output window for data bindings, on a per-binding basis.

The Code

The following markup demonstrates how to use the PresentationTraceSource.TraceLevel property in two different bindings. One of the bindings is valid and binds the value of the text block to the width of the parent grid; the other is invalid and attempts to bind the width of the parent grid to the height of the text block. Set the values of the PresentatonTraceSource.TraceLevel attached properties to see how they behave.

<Window

  x:Class="Recipe.Window1"

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"

  Title="Recipe_01_17"

  Height="300"

  Width="300">

   <Grid x:Name="gdLayoutRoot">

    <Viewbox>

      <TextBlock x:Name="tbkTextBlock">

        <TextBlock.Text>

          <Binding

            ElementName="gdLayoutRoot1"

            Path="ActualWidth"

            diagnostics:PresentationTraceSources.TraceLevel="High"

          />

        </TextBlock.Text>

      </TextBlock>

    </Viewbox>

  </Grid>

</Window>

The element named gdLayoutRoot1 won’t be found, there will be some error message in the OutPut window.

posted @ 2011-03-10 17:02  Further  阅读(282)  评论(0编辑  收藏  举报