DataBinding is one of the most powerful features in WPF. But because it resolves the bindings at runtime and does not throw exceptions, it's sometimes hard to find the reason why the data do not appear as expected. There are mainly two reasons:

  • The DataBinding expression is invalid. Then use Trace Output to resolve.
  • The DataBinding expression is valid, but the result is not the expected. Then use a Debug Converter to resolve it.

Method 1: Trace messages in the output window

In the example, the text property of the TextBlock is bound to the property "InvalidPath" of the StackPanel - which does not exists.

  1. <Window x:Class="DebugDataBinding.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  4. <StackPanel x:Name="stack">
  5. <TextBlock Text="{Binding ElementName=stack, Path=InvalidPath}" />
  6. </StackPanel>
  7. </Window>

In this case the invalid databinding expression is reported by a trace message in the output window

System.Windows.Data Error: 39 : BindingExpression path error: 'InvalidPath' property not found on 'object' ''StackPanel' (Name='stack')'. BindingExpression:Path=InvalidPath; DataItem='StackPanel' (Name='stack'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Note: Binding to a path of a property that has NULL value is a valid expression and does not generate an error message (for e.g. binding to a property of the data context that is NULL).

Adjust the trace level (.NET 3.5 and higher)

NET3.5 has a new feature that allows you to set the level of details of trace messages to NoneLowMedium or High.

To set the trace level you have to include an extra namespace to your XAML:

  1. <Window x:Class="DebugDataBinding.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase">
  5.  
  6. <StackPanel x:Name="stack">
  7. <TextBlock Text="{Binding ElementName=stack, Path=InvalidPath,
  8. diag:PresentationTraceSources.TraceLevel=High}" />
  9. </StackPanel>
  10. </Window>

The following snipped shows how to adjust the trace level by code:

  1.  
  1. PresentationTraceSources.DataBindingSource.Listeners.Add(
  2. new ConsoleTraceListener());
  3.  
  4. PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.All;

Method 2: Use a ValueConverter to break into the debugger

A simple trick is to write a value converter that does nothins except breaking into the debugger. All you need to do now is to add this converter to the binding expression that fails and you can easily see the values that should be bound.

  1.  
  1. /// <summary>
  2. /// This converter does nothing except breaking the
  3. /// debugger into the convert method
  4. /// </summary>
  5. public class DatabindingDebugConverter : IValueConverter
  6. {
  7. public object Convert(object value, Type targetType,
  8. object parameter, CultureInfo culture)
  9. {
  10. Debugger.Break();
  11. return value;
  12. }
  13.  
  14. public object ConvertBack(object value, Type targetType,
  15. object parameter, CultureInfo culture)
  16. {
  17. Debugger.Break();
  18. return value;
  19. }
  20. }

To use the converter in XAML, reference the namespace of the assembly that contains the converter and add an instance of it to the resources of your window.

  1.  
  1. <Window x:Class="DebugDataBinding.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:DebugDataBinding"
  5. Title="Window1" Height="" Width="">
  6.  
  7. <Window.Resources>
  8. <local:DatabindingDebugConverter x:Key="debugConverter" />
  9. </Window.Resources>
  10.  
  11. <StackPanel x:Name="stack">
  12. <TextBlock Text="{Binding ElementName=stack, Path=ActualWidth,
  13. Converter={StaticResource debugConverter}}" />
  14. </StackPanel>
  15. </Window>

以上原文链接

  1.  

另外还有一些可选的配置文件:

  1. <configuration>
  2.  
  3. <system.diagnostics>
  4. <sources>
  5.  
  6. <!--<source name="System.Windows.Data" switchName="SourceSwitch" >
  7. <listeners>
  8. <add name="textListener" />
  9. </listeners>
  10. </source>-->
  11.  
  12. <!--<source name="System.Windows.DependencyProperty" switchName="SourceSwitch" >
  13. <listeners>
  14. <add name="textListener" />
  15. </listeners>
  16. </source>-->
  17.  
  18. <!--
  19. <source name="System.Windows.Freezable" switchName="SourceSwitch" >
  20. <listeners>
  21. <add name="textListener" />
  22. </listeners>
  23. </source>
  24. -->
  25.  
  26. <!--
  27. <source name="System.Windows.RoutedEvent" switchName="SourceSwitch" >
  28. <listeners>
  29. <add name="textListener" />
  30. </listeners>
  31. </source>
  32. -->
  33.  
  34. <!--
  35. <source name="System.Windows.Media.Animation" switchName="SourceSwitch" >
  36. <listeners>
  37. <add name="textListener" />
  38. </listeners>
  39. </source>
  40. -->
  41.  
  42. <!--
  43. <source name="System.Windows.NameScope" switchName="SourceSwitch" >
  44. <listeners>
  45. <add name="textListener" />
  46. </listeners>
  47. </source>
  48. -->
  49.  
  50. <!--
  51. <source name="System.Windows.ResourceDictionary" switchName="SourceSwitch" >
  52. <listeners>
  53. <add name="textListener" />
  54. </listeners>
  55. </source>
  56. -->
  57.  
  58. <!--
  59. <source name="System.Windows.Markup" switchName="SourceSwitch" >
  60. <listeners>
  61. <add name="textListener" />
  62. </listeners>
  63. </source>
  64. -->
  65.  
  66. <!--
  67. <source name="System.Windows.Documents" switchName="SourceSwitch" >
  68. <listeners>
  69. <add name="textListener" />
  70. </listeners>
  71. </source>
  72. -->
  73. </sources>
  74.  
  75. <switches>
  76. <add name="SourceSwitch" value="Off" />
  77. <!--<add name="SourceSwitch" value="All" />-->
  78.  
  79. <!--<add name="SourceSwitch" value="Verbose" />-->
  80. <!--<add name="SourceSwitch" value="Warning" />-->
  81. <!--<add name="SourceSwitch" value="Activity" />-->
  82. </switches>
  83.  
  84. <sharedListeners>
  85. <!-- This listener sends output to the console -->
  86. <add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
  87.  
  88. <!-- This listener sends output to an Xml file named AvTrace.xml -->
  89. <!--<add name="xmlListener" type="System.Diagnostics.XmlWriterTraceListener" traceOutputOptions="None" initializeData="AvTrace.xml" />-->
  90.  
  91. <!-- This listener sends output to a file named AvTrace.txt -->
  92. <!--<add name="textListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="AvTrace.txt" />-->
  93. </sharedListeners>
  94.  
  95. <trace autoflush="true" indentsize="4"></trace>
  96.  
  97. </system.diagnostics>
  98. </configuration>

App.Config 可选配置项

Debug Databinding Issues in WPF的更多相关文章

  1. WPF中的Data Binding调试指南

    大家平时做WPF开发,相信用Visual studio的小伙伴比较多.XAML里面曾经在某些特殊版本的Visual Studio中是可以加断点进行调试的,不过目前多数版本都不支持在XAML加断点来调试 ...

  2. Data Binding in WPF

    http://msdn.microsoft.com/en-us/magazine/cc163299.aspx#S1   Data Binding in WPF John Papa Code downl ...

  3. XML Publisher Report Issues, Recommendations and Errors

    In this Document   Purpose   Questions and Answers   References APPLIES TO: Oracle Process Manufactu ...

  4. 如何 “解决” WPF中空域问题(Airspace issuse)

    空域问题是由于Winform与WPF在底层渲染机制上有所区别而导致的.多数情况下,开发者为了实现不规则的窗体并承载Winform控件时,遇到此类问题.当WPF窗体设置为允许透明(也就是AllowsTr ...

  5. WPF 使用 Edge 浏览器

    原文:WPF 使用 Edge 浏览器 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访 ...

  6. WPF 设置纯软件渲染

    最近看到有小伙伴说 WPF 使用硬件渲染,如何让 WPF 不使用硬件渲染,因为他觉得性能太好了.万一这个版本发布了,产品经理说下个版本要提升性能就不好了.于是就找到一个快速的方法,让程序不使用硬件渲染 ...

  7. 【翻译】WPF中的数据绑定表达式

    有很多文章讨论绑定的概念,并讲解如何使用StaticResources和DynamicResources绑定属性.这些概念使用WPF提供的数据绑定表达式.在本文中,让我们研究WPF提供的不同类型的数据 ...

  8. Why aren't more desktop apps written with Qt?(quora.com系列文章)

    As far as I know and have understood in my experience with Qt, it's a very good and easy to learn li ...

  9. 通过openswan基于Azure平台搭建VPN server

    用过Azure的读者都知道,Vnet一直是Azure比较自豪的地方,尤其是VPN,Azure提供了两种VPN以及专线来保证客户数据的安全性,S2S vpn(站点到站点的,基于IPsec的),P2S v ...

随机推荐

  1. Postman接口调试神器-Chrome浏览器插件

    首先大家可以去这个地址下载 Postman_v4.1.3 这个版本,我用的就是这个版本 http://chromecj.com/web-development/2014-09/60/download. ...

  2. 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型

    前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...

  3. C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...

  4. C#发送邮箱

    之前自己从来没有做过发送邮箱的功能,前段时间项目需要,在找了很多帖子之后,终于实现了. 之后有整理了一下,写了一个类.直接给类传递信息,就可以发送了. 这里还需要说明的是,发送邮箱需要开通POP3/S ...

  5. 根据ip判断返回城市名称查询当地天气

    <?phpheader("content-type:text/html;charset=utf-8");date_default_timezone_set("Asi ...

  6. 彻底搞懂Javascript的“==”

    本文转载自:@manxisuo的<通过一张简单的图,让你彻底地.永久地搞懂JS的==运算>. 大家知道,==是JavaScript中比较复杂的一个运算符.它的运算规则奇怪,容让人犯错,从而 ...

  7. JAVA的内存模型(变量的同步)

    一个线程中变量的修改可能不会立即对其他线程可见,事实上也许永远不可见. 在代码一中,如果一个线程调用了MyClass.loop(),将来的某个时间点,另一个线程调用了MyClass.setValue( ...

  8. 【腾讯bugly干货分享】微信Android热补丁实践演进之路

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1264& ...

  9. 前端构建大法 Gulp 系列 (二):为什么选择gulp

    系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...

  10. Hadoop学习笔记—22.Hadoop2.x环境搭建与配置

    自从2015年花了2个多月时间把Hadoop1.x的学习教程学习了一遍,对Hadoop这个神奇的小象有了一个初步的了解,还对每次学习的内容进行了总结,也形成了我的一个博文系列<Hadoop学习笔 ...