WPF Tips

As WPF is a technology which requires some tips and tricks to use it (probably due to the fact that MVVM was not taken into account from the very beginning of the project) I will store here the tips I found to have them at hand when I need them in the future.

  • Remove close button: as WPF window does not provide the ability to remove the close button the WINAPI must be used. The class at Utils.WPF.WindowBehaviour taken from here provides an attached property that offers this capability.
  • How to bind a PasswordBox: as the WPF PasswordBox is not binddable (due to security risks that I don't care about) I've taken the code from here to be able to bind the password value.
  • Disable Accept button on validation errors: check the property HasValidationError of the ViewModelValidatedBase on the command method that checks command availability.
  • Validate errors: to avoid having a lot of XAML code to add specific validation for each bind the pattern available here has been followed. It allows us to use just ValidateOnDataError=true
  • Change the layout of the StatusBar: as the StatusBar uses a DockPanel by default it's hard to set your controls where you want them. Change the panel to a grid using StatusBar.ItemsPanel as described here.
  • PropertyChanged flows: to create a flow of PropertyChanged events the classes consuming the events will check the name of the events, rename the ones that they need to their property name and write a debug note about the discarded ones. If all the base properties are mapped to the consumer properties the check won't be done to increase performance. We should never have debug messages at the output screen, otherwise it will mean that an error exists.
  • How to open dialogs from the VM: to make the viewmodel independent from the views (and thus they become unit testeable) they will raise events when a dialog has to be shown with the dialog as an event parameter and the dialog result as another parameter.
  • About style organization: 
  • How to make a CheckBox readonly: event when this should be a really easy task as set IsReadOnly there is no such property. To set the check box as readonly you must set the properties IsHitTestVisible and Focusable to False.
  • When to implement PropertyChange events on Model classes: we will implement the event on the model classes that somehow transmit information to the UI if and only if they are updated by other means than the UI itself (timers, network, etc)
  • How to bind a DataGridColumn property: as the columns are not part of the logical or visual tree of the grid they cannot be binded to the same datasource of the Grid. We are using the trick explained here using the Freezable class.
  • How to bind a ConverterParameter: as ConverterParameters are not dependency properties of bindings they cannot be bind. Anyway you can use a MultipleConverter and set the first value to the original binding value and the set second one as what would be the converter parameter. The trick has been taken from here and used in the MultiValueToColorConverter class.

MVVM Flows

Depending on how the data flows from source to view different approaches have to be used. The ones used at the application are the following:

The previous one is used for example with the Options window, the following is the one used with almost all realtime / historical data:

Last edited Feb 18, 2013 at 10:05 AM by somos, version 21


No comments yet.