The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,639,104
since: 19 Jan 2005

WPF Bindings == WTF Bindings?

posted Mon 12 Mar 07

I was recently doing some work on a WPF application doing some pretty standard stuff. I've got a model object graph and I'm binding a piece of my user interface to a specific piece of that graph - very typical, very normal stuff. Here's the rub: I found myself asking WTF?!?! multiple times.

Basically, I wanted to accomplish a simple string binding. I wanted to have a TextBlock object that contains the following text : "You are chatting with (databound name)". Given how powerful and flexible WPF is, you might think that you might be able to do this:

<TextBlock Text="{Binding Path=RemoteName, FormatString=You are chatting with {0}}"/>

Unfortunately, you can't even come close to doing that. Here's where I got pissed off: both Flex (Adobe) and Cocoa (Apple) allow you to directly supply inline format strings. Flex let's you do it through the binding syntax and you can supply format strings directly in Apple's Interface Builder.

Here's where it got nasty. In order to bind this elegantly (and not cheat/hack by creating two textblocks, one bound, one not bound), I had to create a class that implements IValueConverter and then specify that class within the binding syntax so that an instance of that class would be used by WPF to "convert" my value. In other words, I could do this:

<TextBlock Text="{Binding Path=RemoteName, Converter={StaticResource RemoteNameConverter}}"/>

Where RemoteNameConverter is a WPF resource that points to an instance of my RemoteNameStringToStringConverter class (yes, that's what it looks like if you follow the recommended naming conventions).

Maybe I'm having a bad day because of the DST timeshift, or maybe I've been spoiled by Interface Builder letting me do what I want, where I want to do it, or maybe I'm just cranky.. but the fact that WPF won't let me dynamically format databound values directly within the data binding syntax/GUI editor is assinine. Oh well, there's always hope that this will get better in v3.5. Guess I'll have to wait for PDC 2007 to find out.

Points for today's exercise:

  • WPF/.NET Framework v3.0 : 0
  • Cocoa/Objective-C: 1
  • Flex: 1

 

tags:                

links: digg this    del.icio.us    technorati    reddit




1. Rob left...

True. WPF does not have this functionality built in (yet). However, you should read these two posts by Petzold concerning a solution: http://www.charlespetzold.com/blog/2006/03/200923.html http://www.charlespetzold.com/blog/2006/03/310910.html


2. Kevin Hoffman left...
Tue 13 Mar 07 6:13 am

That "solution" is exactly what I mentioned in my blog post. The notion that I would have to create a new class for every type of string I want formatted that has a Convert method is just silly. This kind of thing should be baked into the product, regardless of whether it's a "v1.0" or not.


3. Rob left...

Kevin, you'll have to look at the code a little closer. He's not creating a class for every scenario. He has created a class that encapsulates all of the functionality of string.Format. The first class is for the basic scenario that includes only one parameter, while the second class allows for n parameters. I don't disagree with you fundamentally, this sort of feature should not have been left out of the framework itself, but its nice to know that it's still easy to accomplish.


4. Kevin Hoffman left...
Tue 13 Mar 07 8:50 am

Yes, you're right. He uses a fairly flexible FormattedStringConverter class in order to accomplish this, so it's not horrible. I'm simply saying that the fact that one must go and create this class and then re-use it across every subsequent WPF project reminds me of the days when all developers carried a library of "tools" they had developed to compensate for the lack of tools provided by their language of choice (anyone remember toting their C string libraries around like garlic in a vampire mausoleum?).


5. n4cer left...
Sat 17 May 08 10:19 am

Inline string formatting is now supported in recently released .NET 3.5 SP1 Beta. Check http://dotnet.org.za/rudi/archive/2008/05/16/using-binding-stringformat.asp x for a usage example.


Tag Related Posts

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

CLINQ v1 Demo - Network Message Filtering

Wed 09 Jan 08 7:47 P GMT-05
tags:        

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

Building a Ledger Style for WPF Grids

Tue 21 Aug 07 3:30 P GMT-05
tags:    

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

My first "Acropolis" Application

Mon 04 Jun 07 1:40 P GMT-05
tags:      

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05