tags:

views:

947

answers:

7

This question intends to provide a list of solutions to common pitfalls, "gotcha's", or design issues when developing WPF applications. This can also include proper design-patterns as long as there is an explanation as to why it works best. Responses should be voted up or down based on how common the type of issue is. Here are the rules:

  • One response per post. This will clearly give the most common issues the highest ranking.
  • It would be best to provide the link to the a related post or solution already living somewhere in SO land.
+11  A: 

Problem : The major issue I have seen so far is that people start coding in WPF with the winform UI model in mind.

Solution: WPF is not WinForms/MFC/Win32 So Forget all the UI side assumptions and norms you have used and learned while developing Windows based UI for last 20+ years.

It is very important to understand the core ideas behind this platform, This link- Major UI Development Breakthroughs in the new WPF platform will give an in depth view of WPF. Which lists out the following points. The highlighted ones are my favorite features of this platform.

  1. Advanced Graphics
  2. Drawing Object Model
  3. Rich Application Text
  4. Adaptable UI Layout
  5. Flexible Content Model
  6. Lookless Controls
  7. Data-Driven UI
  8. Consistent Styles
  9. Triggers
  10. Declarative Programming
Jobi Joy
You should really be more specific. I'm down voting until you provide a more detailed explaination of what you mean. You should provide references to maybe some M-V-VM stuff.
Micah
He's absolutely right, and it's the one thing that you have to get over to understand WPF
Paul Betts
Seconded Micah, Which assumptions should I discard?
Jimmy
+1  A: 

Getting data binding to work properly between properties defined in ContentControls (Windows, UserControls, etc..) and properties on elements that make up the controls content. For example. Let's say I have a Window that looks like this:

<Window x:Name="MyWindow"....>
   <TextBlock Text="{Binding Path=PropertyDefinedInMyWindow}" />
</Window>

Problem: No matter how often you update the "PropertyDefinedInMyWindow" it never gets reflected in the TextBlock. SO Question

Solution: You need to set the DataContext of the Window or tell the binding which element the property lives on. SO Solution

Micah
+1  A: 

Problem/Question: SO Question

How do I expose a DependencyProperty of a component in my user control to users? There are plenty of examples of how to expose a normal property by creating a new dependency property and binding, but none on how to expose a read-only property like FrameworkElement.ActualWidthProperty.

Solution: You need to expose a new Readonly DependencyProperty in your user control, and update it whenever your contained "component"'s ActualWidthProperty gets updated. This requires using DependecyPropertyDescriptor to get notified of changes that occur. SO Solution

Micah
+2  A: 

Problem: Using the M-V-VM design pattern, where do I instantiate the views? Does this happen in the ViewModel? SO Question 1, SO Question 2

Solution: WPF development is most effective when using the M-V-VM pattern as opposed to other common patterns such as M-V-C. The tendency is to treat the ViewModel the same as you would the controller which would handle opening and creating views as well as the models. This is not the case in M-V-VM. Views are the only place where are the views should be created. The ViewModels should know nothing of the view. SO Answer 1, SO Answer 2

Micah
This creates a follow-up-question: how do you create the views? I use RelayCommands to bind actions from the view to the ViewModel, so the view does not even know an action has fired, does not know he should open a new view. Solution: create an event in the VM for the View to subscribe to?
Sam
@Sam - I'm going to write up an article about this. Can you give me some examples where the commands should live in the VM versus the View itself?
Micah
A: 

Ivan Towlson did a really good presentation on this topic. Most of the information is in his slides, which you can get from here: http://hestia.typepad.com/flatlander/2008/08/codecamp-2008-.html

Jamie Penney
A: 

Not realising how bad the font rendering is at the start of a project and being told by the client they can't stand looking at it because of how fuzzy everything looks.

don't know why people down vote this. This has been a MAJOR issue for a lot of people. It seems that only after Microsoft tried to use WPF for VS 2010 did they realize how important of an issue it was.
Patrick Klug
A: 

Using code - behind in views, which makes baby FSM cry.

Turing Complete