Tuesday, July 15, 2008

WinForms and WPF. Great alone, yet better together

DevCity.Net - WinForms and WPF Interop - The Best of Both Worlds

Introduction, Code and Markup

So here's the kind of scenario I have in mind.   You have a Windows Forms project on the go and you want to create a ListBox that has more than just text in it.  You want to have an image at the left hand side and two sets of text at the right, each string in a different size font and a different color.

Of course you could use Owner Draw, MeasureString, DrawString, DrawImage and a bunch of other hokey stuff to do this in WinForms.   And before WPF came on the scene that was all you had in your toolbox.

But now that WPF is here you can create ListBoxes that are far more complex than this one.  Mind-boggling complexity and knock-your-eyes-out design characteristics, actually.   To be truthful though you  really need Expression Blend as well as Visual Studio 2008 (or VB 2008)  if you want to create anything that has a lot of sub-elements and/or many variations of colors, such as rotated gradients.

If you're not ready to abandon Windows Forms totally (and why should you?) then you can get most of the best of both worlds and port those fancy design WPF controls into your Windows Forms. 

On the assumption that not everyone has Blend or the full edition of VS 2008 I'm going to limit the ListBox  to something quite basic and will leave it to you to take it from there upwards in the design stakes.   The demo project for this article is written using VB  2008 Express Edition.  This article isn't going to be a full primer on creating WPF controls, but I hope to show you enough detail for you to be able to try the approach out yourself - pushing the envelope as your WPF knowledge grows.

…”

I think this is the tutorial I’ve been looking for since VS 2008 came out, with it’s cool WinForm/WPF support. I’ve dropped a few WPF ElementHost’s/User Controls onto my WinForms but then had no clue what next to do.

This 17 printed page article provides some good, yet simple, cut-n-paste code to you you started, and then best of all, does adds some analysis of that code providing people new to WPF (cough… like me in spades… cough…) some idea of what it does and why it’s important.

No comments: