Home‎ > ‎

WPF Tips

  • Code the UI as a data structure first, then bind to it
    Before you write XAML, start creating classes that represent the entities and values you want in the UI. Make them implement INotifyPropertyChanged and put all of the reflexive logic in these (such as enforcing options based on selections in other parts of the UI). Write unit tests for this behavior. Now you can start {Binding} these structures to XAML
  • Consider starting with a Session class
    Don't make your UI-bound data structures belong to Window1, make them belong to a new class called Session and bind that to the first <Grid> control's DataContext. Now it'll be easier to migrate the UI into another program, support multiple sessions, or save the whole UI state by serializing instances of Session to disk
  • Don't manipulate the controls, manipulate the bound data
    If you can, avoid turning to hacks to discover the offset of a ListView or relying on the index position of a combo-box. And instead of doing "UserChoice = this.MyCombobox.SelectedItem" try doing "<ComboBox SelectedItem="{Binding Path=UserChoice}"..."
Comments