Skip to content

Commit

Permalink
Progress in batch finally works better.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomArrow committed Jan 25, 2021
1 parent f1e9086 commit 379c9be
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
6 changes: 3 additions & 3 deletions BatchProgress.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public partial class BatchProgress : Window



public ObservableCollection<ProgressItem> ProgressStrings
public FullyObservableCollection<ProgressItem> ProgressStrings
{
get { return progressStrings; }
}

public ObservableCollection<ProgressItem> progressStrings = new ObservableCollection<ProgressItem>();
public FullyObservableCollection<ProgressItem> progressStrings = new FullyObservableCollection<ProgressItem>();
public BatchProgress()
{
InitializeComponent();
Expand Down Expand Up @@ -76,7 +76,7 @@ public void RemoveProgressItem(int id, string message="...")
}
}

public class ProgressItem
public class ProgressItem : INotifyPropertyChanged
{
public int id;
public string ProgressText
Expand Down
118 changes: 118 additions & 0 deletions Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,130 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Numerics;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

namespace ColorMatch3D
{

// This class is from: https://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop
public class FullyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
/// <summary>
/// Occurs when a property is changed within an item.
/// </summary>
public event EventHandler<ItemPropertyChangedEventArgs> ItemPropertyChanged;

public FullyObservableCollection() : base()
{ }

public FullyObservableCollection(List<T> list) : base(list)
{
ObserveAll();
}

public FullyObservableCollection(IEnumerable<T> enumerable) : base(enumerable)
{
ObserveAll();
}

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove ||
e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (T item in e.OldItems)
item.PropertyChanged -= ChildPropertyChanged;
}

if (e.Action == NotifyCollectionChangedAction.Add ||
e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (T item in e.NewItems)
item.PropertyChanged += ChildPropertyChanged;
}

base.OnCollectionChanged(e);
}

protected void OnItemPropertyChanged(ItemPropertyChangedEventArgs e)
{
ItemPropertyChanged?.Invoke(this, e);
}

protected void OnItemPropertyChanged(int index, PropertyChangedEventArgs e)
{
OnItemPropertyChanged(new ItemPropertyChangedEventArgs(index, e));
}

protected override void ClearItems()
{
foreach (T item in Items)
item.PropertyChanged -= ChildPropertyChanged;

base.ClearItems();
}

private void ObserveAll()
{
foreach (T item in Items)
item.PropertyChanged += ChildPropertyChanged;
}

private void ChildPropertyChanged(object sender, PropertyChangedEventArgs e)
{
T typedSender = (T)sender;
int i = Items.IndexOf(typedSender);

if (i < 0)
throw new ArgumentException("Received property notification from item not in collection");

OnItemPropertyChanged(i, e);
}
}

/// <summary>
/// Provides data for the <see cref="FullyObservableCollection{T}.ItemPropertyChanged"/> event.
/// </summary>
public class ItemPropertyChangedEventArgs : PropertyChangedEventArgs
{
/// <summary>
/// Gets the index in the collection for which the property change has occurred.
/// </summary>
/// <value>
/// Index in parent collection.
/// </value>
public int CollectionIndex { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ItemPropertyChangedEventArgs"/> class.
/// </summary>
/// <param name="index">The index in the collection of changed item.</param>
/// <param name="name">The name of the property that changed.</param>
public ItemPropertyChangedEventArgs(int index, string name) : base(name)
{
CollectionIndex = index;
}

/// <summary>
/// Initializes a new instance of the <see cref="ItemPropertyChangedEventArgs"/> class.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="args">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
public ItemPropertyChangedEventArgs(int index, PropertyChangedEventArgs args) : this(index, args.PropertyName)
{ }
}


static class Helpers
{





static public BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
Expand Down

0 comments on commit 379c9be

Please sign in to comment.