boo
boo

Reputation: 183

Good practice or bad practice

i am new to WPF and i have one question(the first one actually, more to come) Is it better to add some logic, for example the bindings for the window, in xaml or in the code behind.

Upvotes: 2

Views: 299

Answers (3)

Dave Markle
Dave Markle

Reputation: 97701

If you can put it in XAML, it's generally better to do that than be wiring up stuff in your codebehind.

For one thing, this lets you use tools like Expression Blend more effectively, since you have more stuff available at design time. It also further shifts view logic into the view itself, and helps you stay away from stuffing view-based code in your viewmodels or controllers.

Upvotes: 1

Robaticus
Robaticus

Reputation: 23157

It's kind of a religious debate right now. With an MVVM approach, you can essentially get away with the only code in your codebehind being the creation of your ViewModel and its assignment to your DataContext.

Even all of your event handling can be managed in your ViewModel using UI Commands.

Upvotes: 0

Daniel Plaisted
Daniel Plaisted

Reputation: 16744

Most people wouldn't consider a binding to be logic. Bindings should generally go in xaml. It is a good idea to put logic in a separate ViewModel class which you bind to.

Upvotes: 0

Related Questions