Tuesday, 10 September 2013

WPF / C# Updating name change of an item in a ListBox with ObservableCollections

WPF / C# Updating name change of an item in a ListBox with
ObservableCollections

I have a list box:
<ListBox x:Name="lbxAF" temsSource="{Binding}">
that gets its data from this from this modified Observable Collection:
public ObservableCollectionEx<FileItem> folder = new
ObservableCollectionEx<FileItem>();
which is created within a class that uses FileSystemWatcher to monitor a
specific folder for addition, deletion and modification of files.
The ObservableCollection was modified (hence the Ex at the end) so that I
can modify it from an outside thread (code is not mine, I actually did
some searching through this website and found it, works like a charm):
// This is an ObservableCollection extension
public class ObservableCollectionEx<T> : ObservableCollection<T>
{
// Override the vent so this class can access it
public override event
System.Collections.Specialized.NotifyCollectionChangedEventHandler
CollectionChanged;
protected override void
OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs
e)
{
using (BlockReentrancy())
{
System.Collections.Specialized.NotifyCollectionChangedEventHandler
eventHanlder = CollectionChanged;
if (eventHanlder == null)
return;
Delegate[] delegates = eventHanlder.GetInvocationList();
// Go through the invocation list
foreach
(System.Collections.Specialized.NotifyCollectionChangedEventHandler
handler in delegates)
{
DispatcherObject dispatcherObject = handler.Target as
DispatcherObject;
// If the subscriber is a DispatcherObject and
different thread do this:
if (dispatcherObject != null &&
dispatcherObject.CheckAccess() == false)
{
// Invoke handler in the target dispatcher's thread
dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind,
handler, this, e);
}
// Else, execute handler as is
else
{
handler(this, e);
}
}
}
}
}
The collection is made up of these:
public class FileItem : INotifyPropertyChanged
{
public string Name { get; set; }
public string Path { get; set; }
}
which allow me to store names and paths of files.
Everything works great as far as deletion and addition of files, and the
List Box gets updated flawlessly with respect to those two... however, if
I change the name of any of the files, it doesn't update the list box. I
am still able to manipulate the file, but the name that is displayed in
the list box is different from the new name.
I can't figure out how to update that Name property change.

No comments:

Post a Comment