且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测破碎的WPF数据绑定?

更新时间:2022-10-15 23:38:53

***我可以找到...



如何调试WPF绑定? By Beatriz Stollnitz



由于每个人都不能一直关注输出窗口寻找绑定错误,所以我喜欢选项#2。哪个是添加到您的App.Config

 <?xml version =1.0encoding =utf-8? > 
< configuration>
< system.diagnostics>
< sources>
< source name =System.Windows.DataswitchName =SourceSwitch>
< listeners>
< add name =textListener/>
< / listeners>
< / source>

< / sources>
<开关>
< add name =SourceSwitchvalue =全部/>
< / switches>

< sharedListeners>
< add name =textListener
type =System.Diagnostics.TextWriterTraceListener
initializeData =GraveOfBindErrors.txt/>
< / sharedListeners>

< trace autoflush =trueindentsize =4>< / trace>

< /system.diagnostics>
< / configuration>

配合一个好的正则表达式扫描脚本来提取相关信息,您可以偶尔运行输出文件夹中的GraveOfBindErrors.txt

  System.Windows.Data错误:35:BindingExpression路径错误:'MyProperty'属性在'object'''''MyWindow'(Name ='')'上找不到。 BindingExpression:Path = MyProperty; DataItem ='MyWindow'(Name =''); target元素是'TextBox'(Name ='txtValue2');目标属性为'文本'(类型'String')


While trying to answer a question in the vicinity 'Unit Testing WPF Bindings' I had the following niggling question..
What's the best way to find if you have WPF Data Binding wiring setup incorrectly (or you just broke something that was wired up correctly) ?

Although the unit-testing approach seems to be like Joel's 'ripping off your arm to remove a splinter'.. I am looking around for easier less Overhead ways to detect this.

Everyone seems to have committed themselves to data binding in a big way with WPF.. and it does have its merits.

Best I could find...

How can I debug WPF Bindings? by Beatriz Stollnitz

Since everyone can't always keep one eye on the Output Window looking for Binding errors, I loved Option#2. Which is add this to your App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.Windows.Data" switchName="SourceSwitch" >
        <listeners>
          <add name="textListener" />
        </listeners>
      </source>

    </sources>
      <switches>
        <add name="SourceSwitch" value="All" />
      </switches>

      <sharedListeners>
        <add name="textListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="GraveOfBindErrors.txt" />
      </sharedListeners>

      <trace autoflush="true" indentsize="4"></trace>

  </system.diagnostics>
</configuration>

Pair that up with a good regex scan script to extract out relevant info, that you can run occasionally on the GraveOfBindErrors.txt in your output folder

System.Windows.Data Error: 35 : BindingExpression path error: 'MyProperty' property not found on 'object' ''MyWindow' (Name='')'. BindingExpression:Path=MyProperty; DataItem='MyWindow' (Name=''); target element is 'TextBox' (Name='txtValue2'); target property is 'Text' (type 'String')