WPF Data Binding Troubleshooting Tips

December 10, 2011 05:05 by wjchristenson2

This week I ran into a WPF data binding problem and spent about a day trying to figure out why it wasn’t working.  A day is a long time to spend on why a “Text” property is not binding to an object’s property which is of the type “String”.  Here’s some quick tips on identifying the source of the data binding problem.

1)  Ensure Data Binding Debugging Information shows in Visual Studio’s Output window.  Visual Studio has the capability of showing verbose data binding trace information.  This can help you get to the bottom of the issue quickly.  In my case, my output window was not showing any data binding information…  You’ll need to adjust some Visual Studio debugging options.  First, ensure that all debugging information is not being sent to the immediate window.  Tools >> Options >> Debugging >> “Redirect all Output Window text to the Immediate Window”.  Uncheck that.  Next, Tools >> Options >> Debugging >> Output Window >> WPF Trace Settings >> Data Binding.  Select an option here.  When I was debugging I wanted to see everything going on so I selected “Verbose”.  Unfortunately, the information did not tell my anything as to why my two-way binding was not updating the underlying object’s property…

 

2)  Understand Dependency Properties and the INotifyPropertyChanged interface.  In order for the UI to properly show object property changes or to perform two-way binding, the property which it binds to needs to be a dependency property or the object needs to implement the INotifyPropertyChanged interface so that the UI properly reflects the object’s property changes and the UI can also update the object’s properties (if two-way binding is enabled).  In my case, my UI control was bound to a dependency property which its type also implemented INotifyPropertyChanged and my two-way binding still didn’t work…

 

3)  Are you using a Converter in the data binding expression?  Sometimes converters can throw exceptions and break the binding.  Put breakpoints in your converter and ensure the conversions are not throwing exceptions and breakpoints are being hit.  In my case, the convert from was being hit, but the convert back (updating the object from the UI via two-way data binding) was not being hit.  So one-way was working, but two-way was not…

 

4)  Are you binding a Custom Control which overrides/overloads the property which you are wanting to bind to?  This was the problem for me.  I was wanting to two-way bind the “Text” property of a custom user control to my object’s property.  The custom control was inheriting from TextBox and they were overloading the “Text” property.  Apparently this broke the two-way data binding.  I figured it out by simply dropping a vanilla TextBox onto my UI and binding it’s “Text” property to my expression and two-way binding worked great.  So, when things get really hairy, simplify what you are doing and add more and more things back into the picture until something breaks.  Troubleshooting 101 for the win.

Bookmark and Share

n-Tier Architecture Introduction - Part 1

July 28, 2011 09:29 by wjchristenson2

Throughout my career, I’ve been a guest speaker and judge at local universities.  I’ve also interviewed many job seekers over the past several years.  As a result, I’ve noticed some areas of weakness new developers have when being thrown out into the “real world” of programming.  You’d be amazed at how many new programmers do not have experience with n-Tier architected applications.

So what is n-Tier architecture?  N-Tier architecture refers to an architecture which breaks up an application into separate layers (tiers).  A tier encapsulates methods, objects, properties, interfaces, technologies, etc to perform a set of predefined tasks.  Each tier is separate and thus can reside on other servers, networks, assemblies, namespaces, classes, etc.

For an example, let’s say we have a Silverlight application.  The Silverlight XAP is the presentation layer.  Our Silverlight application then consumes a web service which creates objects which run queries on our SQL Server.  The web service creates the business objects which are part of our business logic layer.  The objects then query the database through the data access layer.

There are many advantages to breaking up the application into tiers (layers) like this:
1) Avoid changing or recompiling other layers if one layer needs changed.
2) Reduction of complexity.
3) Promotes parallel development
4) Easier to maintain.
5) Works much better with automated testing.
6) Easier to scale.
7) Promotes code/tier reuse.
8) Helps with adoption of new technologies.

In our Silverlight example, we had the presentation tier implemented using Silverlight on the client’s web browser.  The web service, objects, and database interaction were done on the server.  Because these layers were “distributed” among multiple machines, they can be referred to as “physical” layers.  On the other hand, layers separated by assemblies or sets of classes can be thought of as “logical” layers.

Now that we know the basics as to what an n-Tier architected application is, in my next article I will show you how to create an n-Tier enabled ASP.NET web application using logical layers in Visual Studio 2010.

n-Tier Architecture – Part 1
n-Tier Architecture – Part 2

Bookmark and Share

WCF DateTime Serialization

May 11, 2010 02:24 by wjchristenson2

By default, if the programmer does not specify the DateTime kind, .NET WCF services will serialize and deserialize DateTime objects based on the local time zone.  What this means is that if you have a client entering information on the east coast (EST) and your server is in central time (CST), the DateTime values entered by the client will have their hour decremented by 1 when the data is deserialized at the server.

The reason why WCF behaves this way is because it thinks that the DateTime entered on the client is in local time.  As a result, when the value reaches the server, 11:00 AM client local time is converted to 10:00 AM because that is what the local time is on the server.

If this is not what is desired, specifying the kind of the DateTime value appears to alter the serialization/deserialization behavior.  Specifying what kind of date your value is will not alter the underlying value.

Unspecified - No conversion:  returnValue = DateTime.SpecifyKind(value, DateTimeKind.Unspecified)
Local - Time Zone conversions:  returnValue = DateTime.SpecifyKind(value, DateTimeKind.Local) 
UTC – No conversions:  returnValue = DateTime.SpecifyKind(value, DateTimeKind.Utc)

Bookmark and Share

Silverlight 4 Final – Released 4/15/2010

April 18, 2010 05:59 by wjchristenson2

On Thursday 4/15/2010, Microsoft officially released the final version Silverlight 4.  Scott Guthrie gave a 60 minute keynote on Silverlight 4 a few days prior.  If you haven’t watched the keynote, I’d recommend doing so.  I was impressed with the new features and the demos provided.  Silverlight 4 includes a ton of new features. 

Here is a quick list of some of the new features that I found interesting:

  • Tooling – Multi-Targeting Silverlight 3 and 4.  You can also now design within VS 2010.
  • Printing API – You can now print from Silverlight.
  • Right-Click Event Handling – MouseRightButtonUp/Down events are now available.
  • Webcam & Microphone Access – SL can now use the client’s webcam(s) and microphone(s).
  • Mouse Wheel Support – The mouse wheel event was added in SL 3, but now TextBox, ComboBox, Calendar, DatePicker, and the ScrollViewer auto-scroll.  You no longer have to manually handle the event.
  • RichTextArea Control – Provides an area where users can create and edit text and specify bold/italic/underlined/etc text.
  • Google Chrome Support - M$ finally now supports Google Chrome.
  • Implicit Theming – Create a style for a targeted type and all of those types will implicitly use it.
  • Fluid UI Support in the ItemsControl – 3 new states: BeforeLoaded, Loaded, and Unloaded.
  • DataGrid Enhancements – Column relative width sizing refactored.
  • DataBinding Enhancements – Binding can now use StringFormat, TargetNullValue, FallbackValue (e.g. Dates no longer requires ValueConverters to format the date).
  • IDataErrorInfo – When this interface is implemented, it reports data validation errors that a UI can bind to.  Only one property is validated/reported on at a time.
  • INotifyDataErrorInfo – Allows developers to provide custom validation logic server-side via asynchronous routines.  Here is a video on the new data validation features.
  • Silverlight Drop Target – Drag and drop folders/files into your Silverlight application.
  • ViewBox – New control which is used to simplify the resizing of images.
  • Text Trimming – Auto adding of word ellipse (…)
  • Keyboard Access in Full Screen Mode – If SL is in full screen mode – all keyboard input is accepted in Silverlight as long as the application is trusted.
  • Network Authentication – You can now pass separate credentials when connecting with 3rd party service providers.
  • COM Interop – Silverlight can now create COM objects – however this only applies to trusted SL applications.
  • Notification (“Toast”) API – Notifications (like a new Outlook email as arrived) can now be used.
  • Local File Access – SL can now access the user’s “My” folders (e.g. My Documents, etc).  This requires trusted SL application.
  • Elevated Trust Applications – Many new features in SL4 require elevated trust.  This new feature will prompt the user to allow/fully trust the SL application when installed.
  • HTML Hosting with WebBrowser – You can now show and host HTML within your SL application.
  • Clipboard API – SL now has access to copy to and paste from the clipboard.
Bookmark and Share

Fiddler - Inspecting WCF Binary Encoded Messages

November 25, 2009 03:57 by wjchristenson2

Fiddler is the tool of choice when it comes to inspecting WCF messages that utilize an HTTP transport.  In a previous post I mentioned that HTTP WCF messages should be in encoded in a binary format to decrease their size.  The problem you run into when encoding your WCF messages is that when Fiddler attempts to inspect an encoded message, it’s not in a readable format.  To resolve this limitation, you can download a free plug-in which will add an inspector to Fiddler to decode binary WCF message.  Using this new inspector, you can inspect binary encoded WCF messages freely from within Fiddler.

http://code.msdn.microsoft.com/wcfbinaryinspector


Bookmark and Share