I am having difficulties with my binding. The problem is that my controls don't properly update/refresh to show what is in the list that I am binding to. This issue came up a while back but I easily worked around it by simply removing the binding, resetting the binding and then resetting the index. This was kind of a brute force method of updating. Now that my program is growing that creates very messy UI code. I am now getting lost within it!
So, I want to get a handle on the correct way to do this. I've been reading up and it seems like there are three ways to go about this; Observable Collections; Lists; INotifyPropertyChanged. My List currently looks like so:
public class LibraryList<T> : IEnumerable<T> where T : LibraryListItem
{
private List<T> libraryList = new List<T>();
bool duplicate;
public void Add(T t)
{
//Ensure unique
duplicate=false;
foreach (T temp in libraryList)
{
if (t.Guid == temp.Guid)
{
duplicate = true;
break;
}
}
if(!duplicate)
libraryList.Add(t);
}
public void Remove(T t)
{
libraryList.Remove(t);
}
public void Remove(int index)
{
libraryList.RemoveAt(index);
}
public int Count()
{
return this.libraryList.Count();
}
public T this[int pos]
{
get
{
return libraryList[pos];
}
set
{
libraryList[pos] = value;
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return libraryList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return libraryList.GetEnumerator();
}
}
I've been reading from several different sources but here is an article that compares all three at once. As I read the article, simply implementing the INotifyPropertyChanged to my LibraryList will rectify my binding problems. Can anyone comment on this?
Also, note that I am using WPF if that makes a difference. Another complicating factor is that I XML Deserialize into the lists. So, an Observable Collection would have to be capable of that as well.
--EDIT--
I think I understand this a bit better now. ObservableCollections seem to implement both INotifyPropertyChanged and INotifyCollectionChanged. So, my solution may be as simple as utilizing ObservableCollections instead of Lists. However, I am going to assume for the moment that this is not the case so that I can get a better understanding. Here is what I come up with:
public interface LibraryListItem: INotifyPropertyChanged
{
Guid Guid { set; get; }
byte[] ToByteArray();
}
public class LibraryList<T> : INotifyCollectionChanged,IEnumerable<T> where T : LibraryListItem
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
private List<T> libraryList = new List<T>();
bool duplicate;
public void Add(T t)
{
//Ensure unique
duplicate=false;
foreach (T temp in libraryList)
{
if (t.Guid == temp.Guid)
{
duplicate = true;
break;
}
}
if (!duplicate)
{
libraryList.Add(t);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, t));
}
}
public void Remove(T t)
{
int index = libraryList.IndexOf(t);
libraryList.Remove(t);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t, index));
}
public void Remove(int index)
{
T t = libraryList[index];
libraryList.RemoveAt(index);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, t, index));
}
public int Count()
{
return this.libraryList.Count();
}
public T this[int pos]
{
get
{
return libraryList[pos];
}
set
{
libraryList[pos] = value;
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return libraryList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return libraryList.GetEnumerator();
}
private void NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, e);
}
}
Then, each class that I have implementing a LibraryListItem will then have to have the following:
public event PropertyChangedEventHandler PropertyChanged;
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
Does this seem ok? Any benefit to doing it this way as opposed to just using ObservableCollections?

0 answers