23

I have a little program which I made with WinForms and now I want to make it again using WPF.

I'm new to WPF and I read that whatever you can do with XAML, you can also do without it, meaning using code only.

Of course that you don't have any XAML when using Winforms. Can I use the same code for the WPF application that I used for the winforms application and get the same result? Or do I need to create and edit XAML? What are the advantages of using or not using XAML?

Also, considering the past experience using Winforms, should I somehow change the way I'm thinking about design and implementation that worked for Winforms but are not that appropriate for WPF?

its4zahoor
  • 1,709
  • 1
  • 16
  • 23
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 2
    Why do you want to recreate it in WPF? – System Down Sep 23 '13 at 21:10
  • 2
    @SystemDown I'm pretty new to WPF as you realized from my question and I know you can just make everything look better using WPF. Almost no one still creates using Winforms if it's not for some very small, non professional stuff and I want to study a bit more about WPF. – CodeMonkey Sep 23 '13 at 21:14
  • 2
    Since this will be learning experience I would just recreate stuff from scratch. You'll learn more and avoid whatever pitfalls direct translation can get you into. – System Down Sep 23 '13 at 21:18
  • 2
    you cannot reuse winforms code, because the WPF controls are different classes. It does't not matter whether you use C# or XAML – Liero Aug 24 '15 at 12:56

5 Answers5

30

No you can't reuse code from winforms in WPF.

and even if you could, you shouldn't.

whatever you can do with XAML, you can also do without it

You should really use XAML to define the UI, and then use DataBinding and MVVM, which is a much more professional way of development than the traditional procedural winforms approach.

Not using XAML is much more troublesome than using it. It may look intimidating at first but it's a really awesome thing to work with.

Of course that you don't have any XAML when using Winforms

No, of course not. winforms is a really old technology that doesn't support anything. That's why they created the Visual Studio designer, otherwise no one would have ever used winforms for anything, because of the horrendous gargantuan behemoth amount of code required to do anything useful.

Can I use the same code for the WPF application that I used for the winforms application and get the same result?

Probably, by adapting some class names and whatnot, but then you lose the main advantage provided by WPF, which is precisely getting rid of the horrible winforms-like code.

considering the past experience using Winforms, should I somehow change the way I'm thinking about design and implementation that worked for Winforms but are not that appropriate for WPF?

Yes. WPF supports MVVM, and that requires a really different mentality from the traditional winforms approach.

I strongly recommend reading Rachel's Excellent Post about upgrading from winforms to WPF.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • 1
    I noticed the post you linked has some links of it's own about everything you wrote. Do you might have some links to articles or what not of your own which you recommend? I started reading the tutorial at http://www.wpftutorial.net/ Do you happen to know if that's a good source as well? – CodeMonkey Sep 23 '13 at 21:38
  • @YonatanNir first of all, I would recommend going thru Rachel's material (linked above). – Federico Berasategui Sep 24 '13 at 02:47
  • 1
    Winforms supports MVVM, Im using ReactiveUI and I can reuse the ViewModels from Winforms in WPF. – Luis Mar 27 '18 at 21:30
  • @Luis yes, please, show me the winforms equivalent to WPF's `ItemsControl`, please. – Federico Berasategui Mar 28 '18 at 19:50
  • "You should really use XAML to define the UI, and then use DataBinding and MVVM, which is a much more professional way of development than the traditional procedural winforms approach." This statement is not true in the general case. When developing a widget, which content is mostly dynamic, we don't want much XAML. – Number47 Aug 13 '22 at 06:51
3

I tried converting a Winforms app to WPF directly, and it causes way more issues than you need because you are fighting the framework all the time. Read up on MVVM and databinding and use that. Its what WPF is designed around, and comes with several advantages such as testability. You can actually get quite a long way with a few of the simple concepts (databinding, viewmodels etc...) and expand your knowledge as you go, but I'd recommend an understanding of MVVM and databinding in WPF first.

A good framework to get started with would be MVVMLight, but its worth writing the basics without a framework to get to know how things work first.

I seem to remember this being a good set of posts from reed copsey: http://reedcopsey.com/series/windows-forms-to-mvvm/

gmn
  • 4,199
  • 4
  • 24
  • 46
  • I started reading the tutorial at http://www.wpftutorial.net/ Do you happen to know if that's a good source about the subject? – CodeMonkey Sep 23 '13 at 21:39
2

Trying to create WPF controls without XAML is asking for trouble - the whole framework is built around the MVVM pattern which demands that your View be described in a declarative fashion, rather than a procedural fashion. While you can definitely create same UI objects in C# and XAML, doing so in C# will require you to know (in truly excruciating detail) how the framework operates in order to compensate when the state of your UI changes. On the flip side, if you do it XAML (as WPF dictates you are supposed to), then things generally update just fine and without the necessity of working around endless bugs. In short "yes" you can do it in code, but "no" you can't do it in code without losing your sanity.

Andrew
  • 1,482
  • 9
  • 16
1

Actually it depends on how complex is your project. For quite a small project you can make this "conversion" quite simply. But it's not a good practice even because WPF development often is based on MVVM/Prism and they give you lots of advantages. Also the behavior of your UI might be different - it depends on the complexity of your project.

1

I'll 2nd or 3rd or 4th the other answers. Never, ever, ever, ever, ever try to build a WPF app with "Winforms style" code. I did that on my first ever WPF app and it was the biggest mistake I've ever made. What a gigantic mess. The 2nd app I went half-way towards MVVM and that too was a gigantic mess. 3rd app and ever since I've gone 100% MVVM, DI, etc. and 100% by the book WPF and code is super clean.

There isn't really a point in porting Winforms code to WPF any way. The majority of the code you wrote in Winforms is thrown out when you go WPF. Basically the only portable stuff is the business logic. All the UI logic is completely different.

And you'll thank us later because one of the greatest concepts ever about WPF is the separation of UI and business logic. That makes swapping out controls a piece of cake.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86