Search code examples
wpfdata-binding

How to detect broken WPF Data binding?


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.


Solution

  • 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')