Bind data

  • Thread starter csharpula csharp
  • Start date
C

csharpula csharp

Hello,I am still having problems with list<object> binding. How can I
enforce that the gui binded to this list will be updated automatically
as soon as there is update in list values? Thank you very much!
 
M

Marc Gravell

List<T> doesn't support IBindingList/IBindingListView; switch to
BindingList<T> and you should get what you need. Note that this will
support additions/removals/swaps etc, but that property-notifications
(i.e. changes to specific properties of specific items) will only be
supported if your entity (T) implements INotifyPropertyChanged (and
raises property-change notifications as necessary). While individual
property events (PropertyAChanged, PropertyBChanged, etc) are
supported by some implementations, BindingList<T> only uses
INotifyPropertyChanged.

Marc
 
C

csharpula csharp

But how can I after implementing this interface inforce the binding list
of those items to be updated?
 
M

Marc Gravell

Everything daisy-chains together. When the property value changes, you
raise the INotifyPropertyChangee.PropertyChanged event. That is
normally the end of what you need to implement.

For completeness, a bindable list (such as BindingList<T>) which
supports notification (IBindingList.SupportsChangeNotification) should
internally subscribe to (for example) the PropertyChanged event for
the items it holds, and (when it is raised) raise the
IBindingList.ListChanged event with ListChangeType of ItemChanged;
equally, it should raise the ListChanged event (with suitable
change-type) when items are added, removed or re-ordered. Luckily,
BindingList<T> does all of this for you, although it doesn't provide
the optional sort/etc features of IBindingList.
A binding-source / currency-manager will subscribe to the ListChanged
event, raising things like CurrentItemChanged, ItemChanged,
ListChanged, etc.
This will be further notified to any bindings, which will get the
value from the source. So through a potentially complex chain of smoke
and mirrors, the UI should indirectly receive word that a property
changed, and update itself.

A simple example of raising the PropertyChanged event follows.

Marc

class Person : INotifyPropertyChanged
{
public Person() { }
public Person(string name, DateTime dateOfBirth)
{
Name = name;
DateOfBirth = dateOfBirth;
}
private DateTime dateOfBirth;
private string name;

public DateTime DateOfBirth
{
get { return dateOfBirth; }
set { UpdateField(ref dateOfBirth, value, "DateOfBirth"); }
}
public string Name
{
get { return name; }
set { UpdateField(ref name, value, "Name"); }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void UpdateField<T>(ref T field, T value, string
propertyName)
{
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
if (PropertyChanged != null) PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top