Fix ItemsControl not handling collection changed
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Collections.Specialized;
|
||||||
using ObservableComputations;
|
using ObservableComputations;
|
||||||
using PropertyChanged.SourceGenerator;
|
using PropertyChanged.SourceGenerator;
|
||||||
using TerminalUI.Models;
|
using TerminalUI.Models;
|
||||||
@@ -27,6 +28,10 @@ public sealed partial class ItemsControl<TDataContext, TItem>
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (_itemsSource == value) return;
|
if (_itemsSource == value) return;
|
||||||
|
|
||||||
|
if(_itemsSource is INotifyCollectionChanged notifyCollectionChanged)
|
||||||
|
notifyCollectionChanged.CollectionChanged -= SourceCollectionChanged;
|
||||||
|
|
||||||
_itemsSource = value;
|
_itemsSource = value;
|
||||||
|
|
||||||
foreach (var disposable in _itemsDisposables)
|
foreach (var disposable in _itemsDisposables)
|
||||||
@@ -39,17 +44,25 @@ public sealed partial class ItemsControl<TDataContext, TItem>
|
|||||||
if (_itemsSource is ObservableCollection<TItem> observableDeclarative)
|
if (_itemsSource is ObservableCollection<TItem> observableDeclarative)
|
||||||
{
|
{
|
||||||
var consumer = new OcConsumer();
|
var consumer = new OcConsumer();
|
||||||
_children = observableDeclarative
|
var children = observableDeclarative
|
||||||
.Selecting(i => CreateItem(i))
|
.Selecting(i => CreateItem(i))
|
||||||
.For(consumer);
|
.For(consumer);
|
||||||
|
|
||||||
|
children.CollectionChanged += SourceCollectionChanged;
|
||||||
|
_children = children;
|
||||||
|
|
||||||
_itemsDisposables.Add(consumer);
|
_itemsDisposables.Add(consumer);
|
||||||
}
|
}
|
||||||
else if (_itemsSource is ReadOnlyObservableCollection<TItem> readOnlyObservableDeclarative)
|
else if (_itemsSource is ReadOnlyObservableCollection<TItem> readOnlyObservableDeclarative)
|
||||||
{
|
{
|
||||||
var consumer = new OcConsumer();
|
var consumer = new OcConsumer();
|
||||||
_children = readOnlyObservableDeclarative
|
var children = readOnlyObservableDeclarative
|
||||||
.Selecting(i => CreateItem(i))
|
.Selecting(i => CreateItem(i))
|
||||||
.For(consumer);
|
.For(consumer);
|
||||||
|
|
||||||
|
children.CollectionChanged += SourceCollectionChanged;
|
||||||
|
_children = children;
|
||||||
|
|
||||||
_itemsDisposables.Add(consumer);
|
_itemsDisposables.Add(consumer);
|
||||||
}
|
}
|
||||||
else if (_itemsSource is ICollection<TItem> collection)
|
else if (_itemsSource is ICollection<TItem> collection)
|
||||||
@@ -78,6 +91,10 @@ public sealed partial class ItemsControl<TDataContext, TItem>
|
|||||||
RerenderProperties.Add(nameof(Orientation));
|
RerenderProperties.Add(nameof(Orientation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => RequestRerenderForThis();
|
||||||
|
|
||||||
|
private void RequestRerenderForThis()
|
||||||
|
=> ApplicationContext?.RenderEngine.RequestRerender(this);
|
||||||
|
|
||||||
protected override Size CalculateSize()
|
protected override Size CalculateSize()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -76,6 +76,10 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (_itemsSource == value) return;
|
if (_itemsSource == value) return;
|
||||||
|
|
||||||
|
if (_itemsSource is INotifyCollectionChanged notifyCollectionChanged)
|
||||||
|
notifyCollectionChanged.CollectionChanged -= SourceCollectionChanged;
|
||||||
|
|
||||||
_itemsSource = value;
|
_itemsSource = value;
|
||||||
|
|
||||||
foreach (var disposable in _itemsDisposables)
|
foreach (var disposable in _itemsDisposables)
|
||||||
@@ -87,15 +91,13 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
|
|||||||
|
|
||||||
if (_itemsSource is ObservableCollection<TItem> observableDeclarative)
|
if (_itemsSource is ObservableCollection<TItem> observableDeclarative)
|
||||||
{
|
{
|
||||||
((INotifyCollectionChanged) observableDeclarative).CollectionChanged +=
|
((INotifyCollectionChanged) observableDeclarative).CollectionChanged += SourceCollectionChanged;
|
||||||
(_, _) => ApplicationContext?.RenderEngine.RequestRerender(this);
|
|
||||||
|
|
||||||
_getItems = () => observableDeclarative;
|
_getItems = () => observableDeclarative;
|
||||||
}
|
}
|
||||||
else if (_itemsSource is ReadOnlyObservableCollection<TItem> readOnlyObservableDeclarative)
|
else if (_itemsSource is ReadOnlyObservableCollection<TItem> readOnlyObservableDeclarative)
|
||||||
{
|
{
|
||||||
((INotifyCollectionChanged) readOnlyObservableDeclarative).CollectionChanged +=
|
((INotifyCollectionChanged) readOnlyObservableDeclarative).CollectionChanged += SourceCollectionChanged;
|
||||||
(_, _) => ApplicationContext?.RenderEngine.RequestRerender(this);
|
|
||||||
|
|
||||||
_getItems = () => readOnlyObservableDeclarative;
|
_getItems = () => readOnlyObservableDeclarative;
|
||||||
}
|
}
|
||||||
@@ -135,6 +137,11 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
|
|||||||
RerenderProperties.Add(nameof(Orientation));
|
RerenderProperties.Add(nameof(Orientation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => RequestRerenderForThis();
|
||||||
|
|
||||||
|
private void RequestRerenderForThis()
|
||||||
|
=> ApplicationContext?.RenderEngine.RequestRerender(this);
|
||||||
|
|
||||||
protected override Size CalculateSize()
|
protected override Size CalculateSize()
|
||||||
{
|
{
|
||||||
InstantiateItemViews();
|
InstantiateItemViews();
|
||||||
|
|||||||
Reference in New Issue
Block a user