I am binding a property of a very straightforward class the Text property of
a control:

public class MySimpleSource
{
  private int myAge;
  public int Age { get { return myAge; } set { myAge = value; } }
}

If I bind multiple controls (e.g. a load of TextBoxes) to this property on a
single instance of this type everything works just great - if I modify the
age in one control the update is successfully propagated to all of the other
controls.  But what I want to know is the 'correct' way of modifying the
value in code and having this same update propagation work.  The following
is obviously insufficient:

  mySource.Age += 1;

because there is no mechanism by which the data binding architecture can
know that I've changed this property.  This seems to work:

  mySource.Age += 1;
  BindingContext[mySource].CancelCurrentEdit();

but I feel kind of unclean after doing that...  I think I'm just exploiting
a side effect here - cancelling an edit will cause all bound control
properties to be forced back to the current value of the data source.  This
doesn't feel like a good way of doing it.

The following also works:

  TypeDescriptor.GetProperties(myDataSource)["Age"].SetValue(
      myDataSource, myDataSource.Age + 1);

but that feels unnecessarily verbose.  And although it seems like less of a
hack from the point of view of making sure the updates happen, the late
binding style is also something I'm not wholly comfortable with.

What I really want is something like this:

  mySource.Age += 1;
  BindingContext[mySource].PropertyChanged("Age");
or even
  BindingContext[mySource].Refresh();

but I don't see any way of doing that.  What's the official solution here?
(Is it: "Don't do that.  Use IBindingList if you want change notifications."
If so, does this mean I can't do change notifications for non-list-like data
sources?  Or should I implement a list with just the one item?..)

The documentation for BindableAttribute indicates that bindable properties
should raise a "property change notification", but it's not entirely clear
how you are supposed to do that.  (Or at least it's not clear how to do it
if your data source isn't list-like.)


--
Ian Griffiths
DevelopMentor

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to