Thursday, September 2, 2010

Changes in x:Array in WPF 4.0

It's been a while, but I thought I'd post the results of a subtle bug I fixed while migrating my app to WPF 4.0. I'm mentioning it because many blogs out there discuss x:Array as a way of creating a static list that you can bind against. There's also a fair amount of confusion because Microsoft started talking about XAML2009 which actually isn't really supported in WPF 4.0 unless you're doing dynamic loading of XAML pages.
What's important to know is that x:Array no longer leaves your Array as an ArrayExtension. Instead, in WPF 4, the array is converted on the fly into a CLR native array of the type your specify in the Type argument.
So if you define something like this in your control Resources block:
<x:Array x:Key="WidgetList" Type="{x:Type local:Widget}" ... />
Then be aware that the result is a Widget[], not an ArrayExtension object.
The practical result is that if you bind against list, say as the ItemsSource in a ListBox, then you no longer should or can add "Items" as the sub-Path.
Before ( < 4.0):
<ListBox ItemsSource="{Binding Source={StaticResource WidgetList}, Path=Items}" ... />
After ( >= 4.0):
<ListBox ItemsSource="{Binding Source={StaticResource WidgetList}}" ... />

1 comments:

Anonymous,  September 5, 2012 at 2:22 AM  

Thanks a lot! You saved us from many hours of wondering.

Post a Comment

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP