Home > 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

Tags: WPF

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

» Next Post: Avoiding Primitive Obsession to tip developers into the pit of success
« Previous Post: WCF: The type provided as the Service attribute could not be found

Comments are closed for this post.

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 ?

© 2005 - 2022 Josh Twist - All Rights Reserved.