Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transitioning from Windows Forms to WPF

For a long time now, I have been stuck with Windows Forms development (started with VB6, and has continued through to C# .NET 4.5), and I have pretty much hit the limit of what Windows Forms can do, both using pure .NET, and special effects with Native Code.

I have tried to learn WPF and XAML, but I get stuck right at WPF's new designer. It really seems very difficult to use in comparison to the Windows Forms designer.

I want to know if there are any alternatives to .NET's WPF designer, that are more suited to Windows Forms developers?

like image 700
Matthew Layton Avatar asked Mar 28 '13 12:03

Matthew Layton


People also ask

How do I convert WinForms to WPF?

1) Target a certain part of your WinForms that you would like to switch up to WPF and then take it out. 2) Replace it with ElementHost. 3) Then in that ElementHost you will have your newly rewritten WPF counterpart. 4) Test it out make sure that it works okay with the rest of the elements.

Is WPF better than Windows Forms?

Winforms vs WPF both are mainly used for the same purpose for developing and designing windows applications, but WPF can be used for the web application. The difference between them is scalability, performance as WPF can render fast compared to windows forms, complexity, and support.

Is WPF still relevant 2021?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

Is WPF harder than WinForms?

It is simple to use WinForms as controls can be used easily, it's less time-consuming and less tricky as compared to WPF.


2 Answers

I like to blog about beginner articles for WPF, and there are a few in particular that may help you out:

  • Understanding the change in mindset when switching from WinForms to WPF
  • What is this "DataContext" you speak of?
  • A Simple MVVM Example

To summarize, the biggest difference between Winforms and WPF is that in WPF your data layer (the DataContext) is your application, while in Winforms your UI layer is your application.

To look at it another way, with WPF your application consists of the objects you create, and you use Templates and other UI objects to tell WPF how to draw your application components.

That's the opposite of WinForms where you build your application out of UI objects, and then supply them with the data needed.

Because of this, the designer isn't actually used that much since your application components are designed in code, and the designer is only needed to draw a user-friendly interface that reflects your data classes (typically Models and ViewModels)

And personally, I prefer to type all my XAML out by hand since it's faster and doesn't make as much of a mess as the drag/drop WPF designer does, although I do use the Designer on occasion to preview what my UI will look like.

So to your answer your question about if there's other WPF designers suited for WinForms developers, I would suggest that instead of looking for another designer, instead look to learn how to use WPF in the way it's meant to be used. Using WPF like it's WinForms means you miss out on much of what makes it so great :)

like image 80
Rachel Avatar answered Sep 19 '22 13:09

Rachel


Well although, some people don't agree, I would also recomment to not use the VS designer. At least not to create an interface. If you may want to get a first impression of your implementation without starting the application, it's a good viewer at least as long no sophisticated things like Styles and Templates are used. But, IMHO, its drag and drop result should only be used as prototype and therefore be discarded after it's no longer needed.

Here are some reasons which are important for me not to use it.

  1. The VS designer is working with fix margins and alignments (which is usually not necessary, if you're using the layout controls), means you have to touch many controls, if the requirements are changed. If you're deep in XAML and the WPF mechanics you can create an applications which can be modified with small effort, regarding the look and feel.

  2. Since the designer is generating the xaml, the composition is not optimal and the UI may perform badly. I didn't measure it, it's just a feeling.

A much better alternative is MS Blend, although the start is everything else but easy. Its drag and drop result is much better that the result of the VS designer.
But it's a pretty powerful tool, which helps you to use pretty powerful elements to create a state of the art UI. I recommend to visit at least a short workshop to get an idea of its opportunities.

Back to your question, IMHO, and I think many people agree, get yourself a good book e.g. WPF Unleashed and later, if you want to know more about the details, WPF Pro. There are a lot of features which are different to Winforms. You won't get to know them by using any designer. I think that's the best approach.

Please also consider that there are many frameworks and libraries (e.g. MVVM light, WPFToolkit) out there, which are already solving some common problems. So it's not necessary to reinvent the wheel.

like image 25
DHN Avatar answered Sep 20 '22 13:09

DHN