Skip to content

Commit

Permalink
Add ItemsSource CollectionViewSource update support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Mar 19, 2019
1 parent 1d40af5 commit 9bf6de6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ public void When_OnItemsSourceChanged()

Assert.AreEqual(1, count);
}

[TestMethod]
public void When_CollectionViewSource()
{
var count = 0;
var panel = new StackPanel();

var cvs = new CollectionViewSource();

var SUT = new ItemsControl()
{
ItemsPanelRoot = panel,
InternalItemsPanelRoot = panel,
ItemTemplate = new DataTemplate(() =>
{
count++;
return new Border();
}),
ItemsSource = cvs
};

Assert.AreEqual(0, count);

cvs.Source = new [] { 42 };

Assert.AreEqual(1, count);
}
}

public class MyItemsControl : ItemsControl
Expand Down
20 changes: 19 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ namespace Windows.UI.Xaml.Controls
public partial class ItemsControl : Control, IItemsControl
{
private ItemsPresenter _itemsPresenter;
private SerialDisposable _notifyCollectionChanged = new SerialDisposable();

private readonly SerialDisposable _notifyCollectionChanged = new SerialDisposable();
private readonly SerialDisposable _notifyCollectionGroupsChanged = new SerialDisposable();
private readonly SerialDisposable _cvsViewChanged = new SerialDisposable();

private bool _isReady; // Template applied
private bool _needsUpdateItems;
Expand Down Expand Up @@ -603,6 +605,22 @@ protected virtual void OnItemsSourceChanged(DependencyPropertyChangedEventArgs e
IsGrouping = (e.NewValue as ICollectionView)?.CollectionGroups != null;
SetNeedsUpdateItems();
ObserveCollectionChanged();
TryObserveCollectionViewSource(e.NewValue);
}

private void TryObserveCollectionViewSource(object newValue)
{
if(newValue is CollectionViewSource cvs)
{
_cvsViewChanged.Disposable = null;
_cvsViewChanged.Disposable = cvs.RegisterDisposablePropertyChangedCallback(
CollectionViewSource.ViewProperty,
(s, e) => {
ObserveCollectionChanged();
SetNeedsUpdateItems();
}
);
}
}

internal int GetGroupCount(int groupIndex) => IsGrouping ? GetGroupAt(groupIndex).GroupItems.Count : 0;
Expand Down

0 comments on commit 9bf6de6

Please sign in to comment.