Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow binding container IsVisible with virtualization. #14276

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Avalonia.Controls/VirtualizingStackPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ private Control GetItemAsOwnContainer(object? item, int index)
generator.ItemContainerPrepared(controlItem, item, index);
}

controlItem.IsVisible = true;
controlItem.SetCurrentValue(Visual.IsVisibleProperty, true);
return controlItem;
}

Expand All @@ -645,7 +645,7 @@ private Control GetItemAsOwnContainer(object? item, int index)
if (_recyclePool?.TryGetValue(recycleKey, out var recyclePool) == true && recyclePool.Count > 0)
{
var recycled = recyclePool.Pop();
recycled.IsVisible = true;
recycled.SetCurrentValue(Visual.IsVisibleProperty, true);
generator.PrepareItemContainer(recycled, item, index);
generator.ItemContainerPrepared(recycled, item, index);
return recycled;
Expand Down Expand Up @@ -684,7 +684,7 @@ private void RecycleElement(Control element, int index)
}
else if (recycleKey == s_itemIsItsOwnContainer)
{
element.IsVisible = false;
element.SetCurrentValue(Visual.IsVisibleProperty, false);
}
else if (KeyboardNavigation.GetTabOnceActiveElement(ItemsControl) == element)
{
Expand All @@ -695,7 +695,7 @@ private void RecycleElement(Control element, int index)
{
ItemContainerGenerator!.ClearItemContainer(element);
PushToRecyclePool(recycleKey, element);
element.IsVisible = false;
element.SetCurrentValue(Visual.IsVisibleProperty, false);
}
}

Expand All @@ -713,7 +713,7 @@ private void RecycleElementOnItemRemoved(Control element)
{
ItemContainerGenerator!.ClearItemContainer(element);
PushToRecyclePool(recycleKey, element);
element.IsVisible = false;
element.SetCurrentValue(Visual.IsVisibleProperty, false);
}
}

Expand Down
97 changes: 97 additions & 0 deletions tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,74 @@ public void Inserting_Item_Before_Viewport_Preserves_FirstRealizedIndex()
Assert.Equal(new(0, 200), scroll.Offset);
}

[Fact]
public void Can_Bind_Item_IsVisible()
{
using var app = App();
var style = CreateIsVisibleBindingStyle();
var items = Enumerable.Range(0, 100).Select(x => new ItemWithIsVisible(x)).ToList();
var (target, scroll, itemsControl) = CreateTarget(items: items, styles: new[] { style });
var container = target.ContainerFromIndex(2)!;

Assert.True(container.IsVisible);
Assert.Equal(20, container.Bounds.Top);

items[2].IsVisible = false;
Layout(target);

Assert.False(container.IsVisible);

// Next container should be in correct position.
Assert.Equal(20, target.ContainerFromIndex(3)!.Bounds.Top);
}

[Fact]
public void IsVisible_Binding_Persists_After_Scrolling()
{
using var app = App();
var style = CreateIsVisibleBindingStyle();
var items = Enumerable.Range(0, 100).Select(x => new ItemWithIsVisible(x)).ToList();
var (target, scroll, itemsControl) = CreateTarget(items: items, styles: new[] { style });
var container = target.ContainerFromIndex(2)!;

Assert.True(container.IsVisible);
Assert.Equal(20, container.Bounds.Top);

items[2].IsVisible = false;
scroll.Offset = new Vector(0, 200);
Layout(target);

scroll.Offset = new Vector(0, 0);
Layout(target);

container = target.ContainerFromIndex(2)!;
Assert.False(container.IsVisible);
}

[Fact]
public void Recycling_A_Hidden_Control_Shows_It()
{
using var app = App();
var style = CreateIsVisibleBindingStyle();
var items = Enumerable.Range(0, 3).Select(x => new ItemWithIsVisible(x)).ToList();
var (target, scroll, itemsControl) = CreateTarget(items: items, styles: new[] { style });
var container = target.ContainerFromIndex(2)!;

Assert.True(container.IsVisible);
Assert.Equal(20, container.Bounds.Top);

items[2].IsVisible = false;
Layout(target);

Assert.False(container.IsVisible);

items.RemoveAt(2);
items.Add(new ItemWithIsVisible(3));
Layout(target);

Assert.True(container.IsVisible);
}

private static IReadOnlyList<int> GetRealizedIndexes(VirtualizingStackPanel target, ItemsControl itemsControl)
{
return target.GetRealizedElements()
Expand Down Expand Up @@ -1160,6 +1228,17 @@ private static TestRoot CreateRoot(
return root;
}

private static Style CreateIsVisibleBindingStyle()
{
return new Style(x => x.OfType<ContentPresenter>())
{
Setters =
{
new Setter(Visual.IsVisibleProperty, new Binding("IsVisible")),
}
};
}

private static IDataTemplate DefaultItemTemplate()
{
return new FuncDataTemplate<object>((x, _) => new Canvas { Width = 100, Height = 10 });
Expand Down Expand Up @@ -1205,6 +1284,24 @@ public ItemWithHeight(int index, double height = 10)
public double Height { get; set; }
}

private class ItemWithIsVisible : NotifyingBase
{
private bool _isVisible = true;

public ItemWithIsVisible(int index)
{
Caption = $"Item {index}";
}

public string Caption { get; set; }

public bool IsVisible
{
get => _isVisible;
set => SetField(ref _isVisible, value);
}
}

private class ResettingCollection : List<string>, INotifyCollectionChanged
{
public ResettingCollection(IEnumerable<string> items)
Expand Down
Loading