Welcome to MSDN Blogs Sign in | Join | Help

UK Application Development Consulting

Your dev. Our passion.
10 reasons to consider WPF for your next desktop application - Reason 8. Interop

This is the 8th post in the series 10 reasons to consider WPF for your next desktop application by Josh Twist, this part explores Interop in WPF.

You can view the other reasons in the series below:

Earlier I introduced a series of posts entitled 10 reasons to consider WPF for your next desktop application. If you haven't read the intro yet, better head on back and read it first.

Other titles I considered for this particular post were:

Reason 8. What!? no datagrid?

or

Reason 8. 1 reason why you should consider WPF for your existing desktop applications.

So let's get started.

What!? No datagrid?

It is a well-known fact that WPF doesn't ship with a built-in DataGrid. Actually, this fact is perhaps the most commonly cited reason that WPF is not suitable for Line Of Business applications. If you're a fan of the datagrid then there is no doubt that this could be a problem for you.
Personally, it wouldn't bother me in the slightest because I'm proud to state that I've never used a DataGrid in any of my Windows Forms applications. I think it's a pretty lazy way of building a UI and a usability sticking point.
So, ignoring my rant above (at your peril;), the omission of a DataGrid doesn't have to rule out WPF.

Thanks to the wonders of the WindowsFormsHost there's nothing to stop you hosting a DataGrid directly inside your WPF application (if you still think you need a DataGrid given the Awesome ListView Control provided in WPF, that is).

<Window x:Class="Reason8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="Reason8" Height="350" Width="700" >
    <DockPanel>
        <my:WindowsFormsHost Name="_windowsFormsHost" >
            <wf:DataGrid x:Name="_gridView" />
        </my:WindowsFormsHost>
    </DockPanel>
</Window>

Here's the glorious hosted DataGrid in all it's glory.

hosted Data Grid

It's even bound to my collection of books...

_gridView.DataSource = _books;

Why not have a play with the ClickOnce Reason 8 Sample and see it for yourself.

Now is a good point to note that the DataGrid isn't the only omission from the WPF control. Others include:

  • DateTimePicker
  • MaskedTextBox
  • MonthCalendar
  • PropertyGrid
These are all useful controls, especially in forms-over-data Line-of-Business applications so you really do need to think carefully before jumping on the WPF express instead of Windows Forms. This reinforces my notes of caution in the introductory post to this series: 10 reasons to consider WPF for your next desktop application.

Of course, you could plug these wholes with third party controls or even write them yourself - but that's precious project time lost.

Does it work the other way around?

Yes, there's a reason I considered calling this post: "1 reason why you should consider WPF for your existing desktop applications": You can host WPF inside of Windows Forms using the ElementHost control:

WinForm's ElementHost control

... and a screenshot to prove it:

WPF inside a Windows Form

Enough interop options to shake at a monkey's armpit.

Originally posted by Josh Twist on 20th November 2007 here.

Posted: Monday, December 01, 2008 9:09 AM by ukadc
Filed under: , ,

Comments

Matthew Adams said:

Interop has a large number of pitfalls, though - especially around animation and transforms, focus management (although isn't that just one big pitfall?), dialog parenting/ownership, opacity etc.

I would argue that for the examples you give, the MS supplied soon-to-be-in-the-box-or-at-least-very-near-it controls are the way to go here, rather than via interop.

(http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14963)

That said, interop is a good story for migration of an existing app towards WPF, or where you have an investment in an existing custom control that you don't have the resources to port right now (though, again, most Win32/MFC custom controls out in the wild are easily replaced with some WPF data templates).

# December 1, 2008 6:29 AM

UK Application Development Consulting said:

This is the 9th post in the series 10 reasons to consider WPF for your next desktop application by Josh

# December 9, 2008 10:56 AM

ukadc said:

Matthew - We totally agree with your comments. Please note that the post was originally published over a year ago and totally predates the codeplex stuff. Josh is considering a '10 Reasons: 1 year on' post as an update his initial series so watch this space!

# December 11, 2008 6:14 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker