Friday, March 12, 2010

Some Notes on Data Binding Windows Forms

Two things I discovered, submitted into the e-ther for the benefit of future googlers.
  1. If you want to drive the state of some radio buttons from a presentation model (or "view model"), put each of them in its own panel. In WPF you have to assign them each a different group name, but the principle is the same: you're disabling the automatic radio button logic so you can handle it in your model (i.e., it is up to you to enforce exclusivity).
  2. If you have a data-bound list box and you want to be able to change the selection in your presentation model and have it reflected in the control (and why not!), you have to bind "SelectedIndex" rather than "SelectedValue". Once I figured this out I could understand why—if you bind to a path or have "ValueMember" set then different list items can have the same value so setting is ambiguous—but it wasn't obvious what was going wrong at first so I thought I'd put it out there.

No comments: