Author Topic: Converting WinForm to WPF Window  (Read 4430 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4088
  • C3D user & customizer
Converting WinForm to WPF Window
« on: October 15, 2019, 10:19:47 AM »
Is it supposed be this difficult? It took me about 6 hours to read & watch a number of learning resources on WPF, then recreate the form to WPFwindow. Visually it appears to be mostly what I need, see images. However, getting all of the comboboxes, checkboxes, text boxes, etc. filled with saved data and to react as needed when one is changed is proving to be a challenge. Why couldn't the developers have used the same naming process for the properties, methods, events, that WinForm controls had when creating WPF?

I've been working on just this 1 form now for 3 days, off & on, and it's still only about 75% done. I have about 90 of these forms to eventually convert, some less involved, some have a lot more controls. Now, you may ask, why am I doing this exercise in futility? Well, thanks to how WinForms don't always resize correctly when used on HiRes monitors with the Windows font scaling set to anything other than 100%, I'm finding it necessary to go this route.

If anyone has any suggestions on how to accomplish this in a bit quicker/smoother fashion please feel free to chime in.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Converting WinForm to WPF Window
« Reply #1 on: October 15, 2019, 12:57:42 PM »
Jeff, I experienced the same frustration when started with WPF. As long time Autocad programmers, I believe most of us were more used to Win Forms. We usually jumps into UI to quickly build a prototype of supposed tool and show it to the business side for further discussion, and we often think through the process in OO way after quite some UI work.

IMO, it would not make too much sense to use WPF without doing it in MVVM pattern (except for super simple UI as dialog box). With MVVM, one should starts with ViewModel. Only when a meaningful ViewModel is available, the view then could be materialize by binding ViewModel data to it. Surely you can hard-code some bind-able properties of the view model with fake/test data, so that the view actually shows as expected, especially if the view has to show a collection of objects, such as ListBox, ListView, Grid,...

As for your particular UI as you showed, since it does not show collection of objects, so you still can go ahead to work on XMAL to make the UI runnable/showable. For combox, you can hard-code combox items in the XMAL for example. But you must get all the data required for this UI into ViewModel and expose them as bind-able properties correctly.

For me, if the UI if modeless window/PaletteSet, I use WPF exclusively. If it is modal UI, I only use Win Form if it is fairly simple, and no action is needed in AutoCAD when the UI is displayed.

One thing is for sure, even one is quite familar with/good at WPF/MVVM, it definitely takes more time to use WPF than to use Win Form. I'd the frustrated feel was quite similar when we VBA programmers first tried .NET API 15 years ago: suddenly you felt not being productive as you used to and so much to learn to do almost the same thing(well, we only realized that we could do more with the pains-taking learning). I am glad I can stick with WPF for most my AutoCAD UIs these days.

Not really suggestion on how to do it, Just share some thoughts/feelings.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Converting WinForm to WPF Window
« Reply #2 on: October 15, 2019, 02:08:10 PM »
I agree with Norman. I started too using WPF as it was WinForms. Still learning and I regularly smash my head on the desk but now  I do not want to go back to WinForms.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Jeff_M

  • King Gator
  • Posts: 4088
  • C3D user & customizer
Re: Converting WinForm to WPF Window
« Reply #3 on: October 16, 2019, 09:50:07 AM »
Thanks guys, I feel a bit better about this knowing that you also felt/feel frustrated at the learning process. I  think I will start wearing a helmet to protect forehead from desk.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Converting WinForm to WPF Window
« Reply #4 on: October 17, 2019, 06:52:39 AM »
I just spilled two days designing a combobox with custom items. I tried to show two lines of text and an image in the combobox and the list below. The opened list showed the items as designed, but the combobox was empty. The code got more and more complex with bindings, converters, style triggers, but nothing helped. Finally I started over without any styling and now it works as designed. Slowly adding styles and binding options now.


Then I styled the tooltip on the items. It pops up on each item in the list but not on the combobox. Moved the tooltip to a datatemplate so it could be used in the combobox and the list below, without the need to design it twice. Then I found out it pops up also when the combobox is not filled. I solved that with a style trigger that sets the TooltipService.IsEnabled when the SelectedItem is null.


But now it works and it looks good. But again I have a blue stain on my forehead from smashing on the desk. Long story short: WPF is horrible to implement, but the result is worth it. It is actually designed for non-programmers. The programmer doesn't need to dive in the design and the designer doesn't see the code. But since we both program and design, we have to work in both worlds.



The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Converting WinForm to WPF Window
« Reply #5 on: October 18, 2019, 03:47:45 PM »
I concur with learning MVVM.  Read about it until your eyes bleed, start by Googling "Josh Smith MVVM".  His ViewModelBase and RelayCommand make up the entirety of my MVVM framework, although full frameworks do exist and may be worth looking into.
Bobby C. Jones

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Converting WinForm to WPF Window
« Reply #6 on: October 27, 2019, 10:17:04 PM »
MVVM Light & Laurent’s (created MVVM Light) Pluralsight course got me started with MVVM. There are a few video courses on WPF MVVM on Pluralsight, I think it’s worth subscribing for a couple of months to get a kick-start. Look into binding and the XAML syntax. StackOverflow is great for all the weird problems you WILL run into.
https://www.pluralsight.com/courses/mvvm-light-toolkit-fundamentals

The initial learning curve is pretty steep but you’ll get there. Don’t be too hard on yourself, refactoring is a thing. I still move cruft out of code behind to its rightful place. Get it done then get it tidy.