|
|
|
@ -4,6 +4,7 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.ObjectModel; |
|
|
|
using System.Linq; |
|
|
|
using Avalonia.Collections; |
|
|
|
using Avalonia.Controls.Presenters; |
|
|
|
using Avalonia.Controls.Templates; |
|
|
|
using Avalonia.Data; |
|
|
|
@ -171,106 +172,28 @@ namespace Avalonia.Controls.UnitTests |
|
|
|
Assert.Equal(new Size(100, 10), target.Scroll.Viewport); |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[InlineData(ItemVirtualizationMode.Simple)] |
|
|
|
[InlineData(ItemVirtualizationMode.None)] |
|
|
|
public void When_Added_Removed_AfterItems_Reset_Should_Work(ItemVirtualizationMode virtMode) |
|
|
|
[Fact] |
|
|
|
public void Containers_Correct_After_Clear_Add_Remove() |
|
|
|
{ |
|
|
|
using (UnitTestApplication.Start(TestServices.StyledWindow)) |
|
|
|
// Issue #1936
|
|
|
|
var items = new AvaloniaList<string>(Enumerable.Range(0, 11).Select(x => $"Item {x}")); |
|
|
|
var target = new ListBox |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string>(); |
|
|
|
|
|
|
|
void create() |
|
|
|
{ |
|
|
|
foreach (var i in Enumerable.Range(1, 7)) |
|
|
|
{ |
|
|
|
items.Add(i.ToString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
create(); |
|
|
|
|
|
|
|
var wnd = new Window() { SizeToContent = SizeToContent.WidthAndHeight }; |
|
|
|
|
|
|
|
wnd.IsVisible = true; |
|
|
|
|
|
|
|
var target = new ListBox() { VirtualizationMode = virtMode }; |
|
|
|
|
|
|
|
wnd.Content = target; |
|
|
|
|
|
|
|
var lm = wnd.LayoutManager; |
|
|
|
|
|
|
|
target.Height = 110;//working fine when <=106 or >=119
|
|
|
|
target.Width = 50; |
|
|
|
|
|
|
|
target.ItemTemplate = new FuncDataTemplate<object>(c => |
|
|
|
{ |
|
|
|
var tb = new TextBlock() { Height = 10, Width = 30 }; |
|
|
|
tb.Bind(TextBlock.TextProperty, new Binding()); |
|
|
|
return tb; |
|
|
|
}, true); |
|
|
|
|
|
|
|
target.DataContext = items; |
|
|
|
|
|
|
|
lm.ExecuteInitialLayoutPass(wnd); |
|
|
|
|
|
|
|
target.Bind(ItemsControl.ItemsProperty, new Binding()); |
|
|
|
|
|
|
|
lm.ExecuteLayoutPass(); |
|
|
|
|
|
|
|
var panel = target.Presenter.Panel; |
|
|
|
|
|
|
|
string itemsToString() => |
|
|
|
string.Join(",", panel.Children.OfType<ListBoxItem>().Select(l => l.Content.ToString()).ToArray()); |
|
|
|
|
|
|
|
void addafter(string item, string newitem) |
|
|
|
{ |
|
|
|
items.Insert(items.IndexOf(item) + 1, newitem); |
|
|
|
lm.ExecuteLayoutPass(); |
|
|
|
} |
|
|
|
|
|
|
|
void remove(string item) |
|
|
|
{ |
|
|
|
items.Remove(item); |
|
|
|
lm.ExecuteLayoutPass(); |
|
|
|
} |
|
|
|
|
|
|
|
addafter("1", "1+");//expected 1,1+,2,3,4,5,6,7
|
|
|
|
|
|
|
|
addafter("2", "2+");//expected 1,1+,2,2+,3,4,5,6
|
|
|
|
|
|
|
|
remove("2+");//expected 1,1+,2,3,4,5,6,7
|
|
|
|
|
|
|
|
//Reset items
|
|
|
|
items.Clear(); |
|
|
|
create(); |
|
|
|
|
|
|
|
addafter("1", "1+");//expected 1,1+,2,3,4,5,6,7
|
|
|
|
|
|
|
|
addafter("2", "2+");//expected 1,1+,2,2+,3,4,5,6
|
|
|
|
|
|
|
|
remove("2+");//expected 1,1+,2,3,4,5,6,7
|
|
|
|
|
|
|
|
var sti = itemsToString(); |
|
|
|
|
|
|
|
var lbItems = panel.Children.OfType<ListBoxItem>().ToArray(); |
|
|
|
|
|
|
|
Assert.Equal("1", lbItems[0].Content); |
|
|
|
Assert.Equal("1+", lbItems[1].Content); |
|
|
|
Assert.Equal("2", lbItems[2].Content); |
|
|
|
Assert.Equal("3", lbItems[3].Content); //bug it's 2+ instead
|
|
|
|
Assert.Equal("4", lbItems[4].Content); |
|
|
|
Template = ListBoxTemplate(), |
|
|
|
Items = items, |
|
|
|
ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Width = 20, Height = 10 }), |
|
|
|
SelectedIndex = 0, |
|
|
|
}; |
|
|
|
|
|
|
|
int lbi = 0; |
|
|
|
Prepare(target); |
|
|
|
|
|
|
|
//ensure all items are fine
|
|
|
|
foreach (var lb in lbItems) |
|
|
|
{ |
|
|
|
Assert.Equal(items[lbi++], lb.Content); |
|
|
|
} |
|
|
|
items.Clear(); |
|
|
|
items.AddRange(Enumerable.Range(0, 11).Select(x => $"Item {x}")); |
|
|
|
items.Remove("Item 2"); |
|
|
|
|
|
|
|
//Assert.Equal("1,1+,2,3,4,5,6,7", sti);
|
|
|
|
} |
|
|
|
Assert.Equal( |
|
|
|
items, |
|
|
|
target.Presenter.Panel.Children.Cast<ListBoxItem>().Select(x => (string)x.Content)); |
|
|
|
} |
|
|
|
|
|
|
|
private FuncControlTemplate ListBoxTemplate() |
|
|
|
|