• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Whenever you search in PBworks or on the Web, Dokkio Sidebar (from the makers of PBworks) will run the same search in your Drive, Dropbox, OneDrive, Gmail, Slack, and browsed web pages. Now you can find what you're looking for wherever it lives. Try Dokkio Sidebar for free.


WPF Best Practices 

Page history last edited by Ryan Riley 14 years, 1 month ago
  1. DataTemplates as Views
    1. Views are commonly implemented with UserControls.
    2. DataTemplates can be stored in Resource Dictionaries and have no code-behind. Once loaded, they can be automatically applied to their respective Presentation Model's by declaring the TargetType property.
    3. DataTemplateSelector classes can be created for selecting the correct template for complex Presentation Models with many possible renderings.
    4. This forces the Presentation Model and not the view to have all of the behavior and logic, which makes the presentation layer more testable.
  2. Prism Project Structure
    1. JB: Current project uses a module approach, but all modules are currently within one assembly.
    2. JB: Modules register services, presenters and presentation models, and views, as well as load a default home page and provide a command for the application bar to navigate to the module.
    3. JB: Currently using Unity to register and resolve, primarily through constructor injection and sometimes as a Service Locator.
  3. Working with a Designer
    1. Claudio: Be careful with hard-coded sizes, fonts, etc.
    2. Claudio & JB: Generally need to clean up XAML created by a design tool such as Expression Blend, Illustrator, etc.
      1. Clean up by manually reviewing all of the code, which may help if you want to extract and centralize some styles that the designer may not know about (which is probably a bad idea).
      2. Clean up using KaXAML's auto-cleanup feature.
  4. First-Timer Tips
    1. Stub out styles and add styles to everything. Global styling for controls will generally end badly for you, so just get into the practice of creating styles for everything, even if your stub is only the default style.
    2. Be careful with 3rd-party tools. They may seem helpful at first, but many of them are still so new that you'll run into enough problems to force you to create a lot of your own controls anyway. That should eventually improve.
    3. JB: Prefer custom controls to styles that require specific styles for nested controls. With a custom control, you can still allow the end-user to change the style, but you'll be able to keep everything consistent.
    4. Claudio: Move Resource Dictionaries into separate .dlls in order to allow skinning by simply swapping the .dll. This poses problems with design tools, so managing this is tricky.
  5. Binding
    1. JB: Debugging can be tricky, but the Test Converter (aka Identity Converter) will allow you to set breakpoints and view the type and value of the bound data.
    2. Jeremy Miller: Used the micro-controller strategy instead of the built-in Data Binding with Expressions to avoid having to implement INotifyPropertyChanged. This library was written as a fluent interface, as well, to make writing the bindings easier to read and remove the binding logic from the XAML, which keeps the XAML purely visual.
    3. Jeremy Miller: Glenn Block is currently working on some ViewModel enhancements with a fluent interface DSL for binding. This should help with static type reflection and large-scale test automation.
  6. Attached Properties and Attached Behaviors
    1. JB: Another approach to removing code behind is to create Attached Properties--a special form of Dependency Properties--that execute behavior, commonly called Attached Behaviors. Add a double-click command binding with a DoubleClickBehavior.
    2. The community at large is not using these much.
    3. Jeremy Miller: Poor navigability from the XAML to the custom control source. RR: This may be fixed in ReSharper 4.5.
    4. Is code-behind so bad? Not always, but it is all too often abused when it's not strongly avoided.
    5. How does one win the "No code-behind" argument?
      1. JB: There's no magic bullet.
      2. RR: Visual design considerations helped, such as favoring stylable ListBoxes and ListViews to 3rd-party data grid controls.
  7. ICommand, DependencyObject, DependencyProperty
    1. JB: ICommand is an extremely simple interfact but very powerful for acting on databound values and objects.
    2. Jeremy Miller: Can lead to some indirection.
    3. What about RoutedEvents? JB: We're not using them for anything right now.
  8. Testing and Test Automation
    1. Jeremy Miller: WPF has a very weak test story right now.
    2. crickets: chirp (translated = agreement all around)
  9. Tools
    1. XAML Power Toys
    2. Expression Blend
    3. KaXAML
  10. WPF Project Pipeline
    1. Some companies are still on Windows 2000 and not ready for WPF.
    2. Questions about the stability of the platform.
    3. Questions about tooling, which is still a work in progress.
    4. While it's not getting a lot of updates from Microsoft, WinForms will still be around for a long time, so it's hard to sell the new platform.
    5. Learning curve? 
      1. JB: 3-4 months to really get it.
      2. JB: Definitely get WPF Unleashed.
      3. RR: Easier migration for a web developer than for a WinForms developer because of the familiarity with markup languages.
    6. Layouts
      1. Not everything is a Grid or Canvas. Knowing layout is an important, if somewhat difficult, concept for being effective with WPF.
      2. JB: Custom layout controls can really help. Use the Grid.IsSharedSizeScope attached property to keep grid spacing in child controls.
      3. RR: Grids are still useful for re-sizable layouts with the GridSplitter, and you can still create additional layouts with other layout panels within each grid cell.
  11. Toolkits
    1. JB: Used Infragistics on a recent project, but most of the controls have been replaced with custom controls as we went along.
    2. Jeremy Miller: Custom, dynamic layouts are an excellent tool to use.
    3. Claudio: Ribbons are difficult and need work.
  12. Frameworks
    1. Composite Application Guidance for WPF and Silverlight (aka Prism)
      1. Good, lightweight framework for building applications. A lot of the parts are easily replacable, unlike CAB. Make it fit your application.
      2. Jeremy Miller: Nothing he doesn't like, but it doesn't solve the hard problems (i.e. EventAggregator).
    2. Caliburn
    3. Sculpture

Comments (0)

You don't have permission to comment on this page.