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}}" ... />


Friday, June 26, 2009

WPF: Sorting Sets of Simple Strings

I've been doing a fair bit of WPF coding recently, and it's a compelling experience. The thing is, it isn't easy, and I think that surprises some people. Part of the reason for this is that the documentation still feels incomplete, in that undocumented kind of way. But the other reason is that it forces you to think very abstractly, all the time. If I were to think of a decent analogy, it would be one involving cooking: Coding in most UI toolkits is like cooking with lots of pre-made dishes (pre-cooked noodles, microwave dinners, pasta sauces out of the bottle.) Sure, you can make a decent meal, but the results are pretty predictable and maybe not that satisfying. Coding in WPF is like cooking with real ingredients - it can be challenging, and you can certainly mess up the results, but you can also whip up something fantastic.

That said, I thought I might pass along a tip for something that seems like it should be quite easy, but the documentation makes it unclear. I needed to produce two comboboxes for some UI, both of which contain lists of Strings. For one combobox, I needed a custom sort pattern, and for the second one I needed a straight up sort.
I'm finding that there are a bunch of ways to do this. You can create a List<> object that contains the strings, call Sort() on it, then bind it. However, that might not be an adequate solution since you're re-arranging your internal data and you may want to preserve its original order.
The other way of doing this is to apply a SortDescription to the Items list of your combobox or ListView. A SortDescription is pair consisting of a String which describes the property to sort on, and a sort order (ascending or descending.)
Now here's a little tip I just figured out: Most examples explain how to Sort a list of items that are bound to the control, by specifying the property name of the property to sort the content on.
    new SortDescriptions("Content", ListSortDirection.Ascending));
But what if your items don't have a sub-property? You may have guessed the answer, but I am hear to confirm that if you pass an empty string as the first parameter to the SortDescription, then it will apply the SortDirection to the Items themselves:
    new SortDescriptions("", ListSortDirection.Ascending));
And voila! For the second combo where I needed a custom sort, I simply did my sort on the items themselves beforehand then added them in the right order.


Friday, June 5, 2009

E3: Let's Get Physical

Seems like the latest E3 was full of surprises... or maybe not. With Nintendo's raging success in the video game market due to its unconvential Wii, you'd have to expect that both Microsoft and Sony would have to respond. They are now officially playing catch-up, and introduced their takes on the genre at the electronics tradeshow.
Microsoft gave a rather bold live demo of its newly acquired Project Natal, a smart camera that can motion-capture participants using a single camera device. This may sound like a gimmick, but the demos are surprising and effective. Included in the system are both image and voice recognition systems that are used to recognize the current user and interpret commands, respectively. I think the possibilities just for the social and device browsing features are pretty cool - built in voice and video chat, voice-activated, as well as being able to browse to your favourite downloaded media using hand motions. Kind of makes Minority Report seem a little clunky, actually.
Probably the most impressive demonstration of the technology is the Milo video, featuring Lionhead and Peter Molyneux.
Thanks to Trevor, who pointed out that Penny Arcade has the best take on Natal:
And then there was the PS3, that stayed with the conventional, and yet refined Nintendo's motion controller with one that appears to be much, much more sensitive. I think this is actually a pretty smart move on their part, since they are now working with a refinement on a proven technology. It remains to be seen whether they can get these controllers to the 15 million or so install base that the PS3 currently enjoys.
I found the handwritting demo to be the most effective. I mentioned to a collegue that the token Sony Demo Geek could write better in virtual space than I can on paper.
And finally, there was Nintendo. You'd think that the company that revolutionized and galvanized the gaming industry would want to follow that up with something that would keep them one step ahead of the competition. With that in mind, Nintendo unveiled Vitality, which is basically a heartbeat sensor that attaches to your finger. “...Intends to have you see the information relating to the inner world of your body," is the quote from Satoru Iwata, president of Nintendo. At this rate, you can soon expect an electronic rectal thermometer from the esteemed "gaming" company in the coming years. With force feedback.


Thursday, May 21, 2009

I'm a Doctor, Jim, not a Horse Lord of Rohan!

I was reading an interview with JJ Abrams on and had my mind blown. I knew the guy who played Bones in the fantastic new Star Trek film looked familiar, but I hadn't bothered to look him up.

However, this quote from JJ certainly opened my eyes:
"When Karl Urban came in, quite frankly I felt that it would be unlikely that that guy from BOURNE or the hunk from LORD OF THE RINGS was going to be Bones. I knew he was from New Zealand. I just didn’t see the connection, even though I liked his work very much, but I thought “Well, he doesn’t seem right for this, but I’m a fan.” He came in and blew my mind so fast. It was one of those great things where it’s a great lesson to not be so closed minded, but God he was amazing. He just channeled DeForest Kelley, it was eerie."
The "hunk" from LoTR? Bones?
Holy crap. Eomer from The Two Towers is Bones? What a performance by Karl Urban.


Thursday, May 7, 2009


What's the opposite of a Karma Slap? Good things happen to people who tip well. I took a cab today to pick up my car and left the cabby a $4 tip for a $19 ride, for no particular reason. I'd consider that a decent tip. As I was waiting for my car, the cabbie came back and dropped off a small brown bag I'd forgotten in the backseat. The weird thing is I thought I had checked that I had everything because I was also carrying my camera case and a chequebook. The package (which didn't look like much more than a work snack, honestly) actually contained some naturopathy pills, which I had just paid $50 for. So there you go.

The cabbie was obviously a good guy, but I'm guessing he's not turning the car around if I give him a twenty note on the nineteen dollar fare.


  © Blogger templates The Professional Template by 2008

Back to TOP