0

I have done quite a bit of research in the last few months and haven't been able to really find a good answer for my question yet:

A little background info: I am new to WPF and was asked to create a project using it. I learned most of the concepts on my own, but am struggling with one major issue. There is just too much code in the MainWindow class.

The reason there is too much code: There is too much code because all of the events for my UI Elements must be controlled in this one class.

Now I just recently discovered the use of partial classes. So I plan to split the events off into a seperate file. I am just wondering if there is any better way to decrease the size of my MainWindow class? I haven't been able to find a way to pass controls between classes, because I know it is not good to pass TextBoxes, or ListBoxes etc. between classes or methods. (I do understand, however, that you can pass parameters such as textBox1.Text or similar properties, but that doesn't fix my issue.

Jordan Carroll
  • 696
  • 2
  • 11
  • 29
  • 4
    Have you looked into use the MVVM model? – ChrisF Aug 01 '12 at 12:32
  • One tip: if you are using lots of event handlers in codebehind, you are probably not using WPF to its full potential. MVVM (as mentioned by @ChrisF) will help with this, but in general I would look on an explicitly-wired-up event handler in XAML as a code smell. – Dan Puzey Aug 01 '12 at 12:43
  • 1
    I can't answer my own questions yet, but if anyone else has a similar question MVVM is the way to go. – Jordan Carroll Aug 01 '12 at 13:23
  • 1
    Watch this tutorial: http://www.youtube.com/watch?v=tKfpvs7ZIyo&feature=related – Jordan Carroll Aug 01 '12 at 13:23

1 Answers1

6

Introducing partial classes will not make your code any better, it will just spread the bad code over different files.

The "standard" way of developing WPF applications is to use the Model-View-ViewModel (MVVM) pattern. Using this enables you to have XAML-only views, view models that control their behavior and models that provide your application data.

There are tons of tutorials on this on the web. MSDN has a good introduction to this pattern. Having read this you should have a good overview of MVVM and can start applying it to your application.

Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53