Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you draw the line between code and XAMLin WPF?

Tags:

c#

.net

mvvm

wpf

xaml

The more I learn about WPF and XAML, the more I realize that you can do pretty much all of your GUI initialization and event handling glue in either XAML or in code (say C# code or VB.Net code).

My question is to those who have been working on WPF for longer and ideally those who have shipped apps with it -- where did you find was the best place to 'draw the line' between XAML and code? Did you use XAML wherever you could? Only where interfacing with non-coding UI designers?

Any tips in this area would be extremely helpful to myself and other coders who are just getting into WPF programming and are kind of paralyzed by all the choices we can make!


2 Answers

One thing that I would look at is the model-view-view model pattern. It is a very elegant pattern which naturally separates everything into nice buckets ... including your xaml.

For example, it helps you keep a clear boundary between the developer and the designer and even allows for test driven development.

There is a whole bunch of info out there on it, but I would start with John Gossman's blog posts:

  • http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx
  • http://blogs.msdn.com/johngossman/archive/2005/10/09/478894.aspx
  • http://blogs.msdn.com/johngossman/archive/2006/02/26/539598.aspx
  • http://blogs.msdn.com/johngossman/archive/2006/02/27/540304.aspx
  • http://blogs.msdn.com/johngossman/archive/2006/03/04/543695.aspx
  • http://blogs.msdn.com/johngossman/archive/2006/04/13/576163.aspx

Update: Just want to point people to another StackOverflow post with lots of good info on M-V-VM in it.

like image 158
cplotts Avatar answered Sep 14 '25 11:09

cplotts


One tip is to not declare event handlers in XAML. Instead, name your elements and attach events handlers in the code-behind. That helps keep a clean separation between the designer and developer.

like image 29
Kent Boogaart Avatar answered Sep 14 '25 10:09

Kent Boogaart