Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 787
Gishu Avatar asked Dec 03 '08 13:12

Gishu


People also ask

How does data binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How many types of binding are there in WPF?

WPF binding offers four types of Binding.

Which class is used for data binding in WPF?

WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.


2 Answers

In .NET 3.5 it was introduced a new way to specifically output tracing information about specific data bindings.

This is done through the new System.Diagnostics.PresentationTraceSources.TraceLevel attached property that you can apply to any binding or data provider. Here is an example:

<Window x:Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"     Title="Debug Binding Sample"     Height="300"     Width="300">     <StackPanel>         <TextBox Name="txtInput" />         <Label>             <Label.Content>                 <Binding ElementName="txtInput"                          Path="Text"                          diag:PresentationTraceSources.TraceLevel="High" />             </Label.Content>         </Label>     </StackPanel> </Window> 

This will put trace information for just that particular binding in Visual Studio's Output Window, without any tracing configuration required.

like image 101
Enrico Campidoglio Avatar answered Sep 22 '22 06:09

Enrico Campidoglio


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') 
like image 29
Gishu Avatar answered Sep 22 '22 06:09

Gishu