Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to effectively debug WPF data bindings?

Tags:

wpf

I am having a data binding problem (a separate question), and normally when I code, I can use the debugger to step through, and hit break points. What is probably a simple issue has taken me all day, with no resolution (getting very frustrated now), because I do not know how to "run the debugger" on my XAML data bindings.

If anyone could explain how to do this, it would be greatly appreciated.

like image 686
Sako73 Avatar asked Nov 10 '11 20:11

Sako73


People also ask

How do I debug WPF application in Visual Studio?

You can use the Live Visual Tree to explore the visual tree of a WPF object and view the WPF dependency properties for the objects in that tree. You can use the WPF Tree Visualizer to explore the visual tree of a WPF object and view the WPF dependency properties for the objects in that tree.

How do I debug XAML?

Navigate to Tools –> Options –> Debugging –> General –> Enable UI Debugging Tools for XAML . In case you are not able to inspect you xaml and not able to see it in Live property explorer you need to check it. With the option selected, the both xaml inspection and live visual tree work seamlessly for any xaml debugging.


2 Answers

I have no idea if VS provides an easy way to debug bindings, but I usually use Snoop for debugging bindings

It's a tool that will go through a WPF application and give you a TreeView of the application's Visual Tree. You can select an element to view its DataContext and other properties. If there are any binding errors, the property is usually highlighted and it will say what the error was. If the binding is failing because the property doesn't exist, I can usually trace the DataContext and figure out where I went wrong in my binding.

like image 124
Rachel Avatar answered Oct 11 '22 12:10

Rachel


add a dummy converter

<local:DebuggerConverter x:Key="DebuggerConverter" />

<TextBlock Text={Binding ToSomething, Converter={StaticResource DebuggerConverter}} />

the converter

public class DebuggerConverter : IValueConverter
{
  #region IValueConverter Members

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    // Set breakpoint here
    return value;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    // Set breakpoint here
    return value;
  }

  #endregion
}

or use this and look at your output window

<Window xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase">
  <TextBlock Text="{Binding ToSomething, diagnostics:PresentationTraceSources.TraceLevel=High}" />
</Window>

hope this helps

like image 35
punker76 Avatar answered Oct 11 '22 12:10

punker76