Closed ScarletKuro closed 1 month ago
For virtualization to work efficiently, the items need to be distinguishable in some way. Therefore, it will not work if your items are not unique. I have added a check so that the user will get a helpful error message.
For virtualization to work efficiently, the items need to be distinguishable in some way. Therefore, it will not work if your items are not unique. I have added a check so that the user will get a helpful error message.
Thanks for the explanation and for the better error message. I wonder if this would be more efficient on paper.
private void VerifyItemsDistinct()
{
var seenItems = new HashSet<object>();
foreach (var item in Items)
{
if (!seenItems.Add(item))
{
throw new InvalidOperationException("Items must be distinct.");
}
}
}
Since HashSet
has O(1) in average for both insertions and lookups, while the Items.Distinct().Count() != Items.Count
should be O(n). But HashSet
would take more memory.
Since Distinct also uses a set internally, as in your suggestion, it should be similarly efficient. There may be a very tiny overhead, but I think it is negligible. Only in the rare error case that the items are not unique would your suggestion really be faster, since it does not have to iterate over all items.
Describe your issue Hello.
I have a reproduction (it's part of production app, I tried to remove as much code as possible):
VirtualizationPanelRepro.zip
If you look at
MainWindow.xaml.cs
, line 245:This line breaks virtualization:
The items are not displayed, and when you try to scroll using the left and right buttons, some items appear. However, when you reach the end, you encounter an exception:
This issue is resolved if you replace
VirtualizingWrapPanel
withWrapPanel
inApp.xaml
, line 462. The items and side scrolling will then work correctly.NB! Honestly, the
Enumerable.Repeat
was for testing purpose as I wanted to see how it works with many items and in real code I don't have it, but I thought you might be interested to fix it as I'm sure someone can be affected by it.Version Info Package Version [e.g. 2.0.11] .NET Version: [e.g. .NET 6] OS Version: [e.g. Windows 10 Build 19045.4894]