Skip Navigation LinksHome > View Post
Databinding to property bags and dictionaries in WPF

My colleague Darren 'BizTalk' 'Cloud' Jefford has been playing around with a bit of WPF recently incorporating it into some of his demos that show off the latest in our cloud based offerings, such as SQL Server Data Services (SSDS).

Data items stored in SSDS, or entities as they're called, are effectively property bags (or dictionaries) and therefore in .NET you access their properties using indexers. Here's an example LINQ query looking for entities where the number of copies sold is less than 1000.

from e in entities
where e["NumberOfCopiesSold"] < 1000
select e;

He wanted to know if was possible databind to an SSDS entity in WPF? How would you specify an indexer property?

et voila:

<TextBox Text="{Binding [NumberOfCopiesSold]}" />

In this instance the DataContext of the TextBox in question. What if an entity was a property of the current DataContext itself, maybe named MyEntityProperty?

<TextBox Text="{Binding MyEntityProperty[NumberOfCopiesSold]}" />

This technique also works fine with numeric indexers including those on arrays

<TextBox Text="{Binding MyArray[3]}" />

Nice - but there is a caveat. There is no notification support for indexers so if you programmatically change the value of your MyEntityProperty[NumberOfCopiesSold] like so...

_myDataContextObject.MyEntityProperty["NumberOfCopiesSold"] = 12;

... the UI won't automatically react to this change.

Also, note that there's no support for this binding syntax in Silverlight 2 either.

For an intro on WPF databinding check out this post: Reason 2 - Databinding

Josh Post By Josh Twist
6:48 AM
13 Aug 2008

Comments:

Posted by Jean-Philippe Leconte @ 14 Aug 2008 10:24 AM
Wouldn't implementing INotifyPropertyChanged and sending "Item[]" as the property name fix the notification support problem ?

Post a comment:

Name  

E-mail (never shared)

URL

Comments  

Captcha ImageRefresh Image
What's this?
Enter code above