Saturday, July 24, 2010

Debugging WPF Binding

  WPF implements the mapping of data models using {Binding} expression. However in some cases there may be difficulties with understanding that the value was received as a result of binding. In this article I want to talk about a possible decision to enable the debug mode to see the used values. The solution is not new and probably many about it know, but for those who started working with WPF it might be useful. The essence of solution lies in the fact that the binding is carried out not directly but through a simple converter that does nothing to transform, but allows you to set Breakpoint while debugging and see the value of object that used.
using System;
using System.Windows.Data;

namespace WpfApplication
{
  public class EmptyConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return value;
    }
  }
}

  This is how data binding is implemented to the element. It creates an object converter and used in the {Binding} expression.
<Window x:Class="WpfApplication.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication"
    x:Name="Window">
 
  <Window.Resources>
    <local:EmptyConverter x:Key="EmptyConverter"/>
  </Window.Resources>
 
  <Label Content="{Binding Path=TestField, ElementName=Window, Converter={StaticResource EmptyConverter}}" /> 
</Window>


No comments:

Post a Comment