A virtualized inventory reuses visible rows while scrolling. Every row must have the same height.
dotnet add package RecursiveAI.OneGame.UI.KitRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneGame.UI; using OneGame.UI.Kit; var items = Enumerable.Range(0, 10000) .ToArray(); var ui = new UiTree(); ui.SetViewport(640, 360); var list = UiKit.TextList(() => items, i => $"Item {i}", Length.Ui(28)); ui.Root.Add( Ui.Column(list).Height(280)); for (var i = 0; i < 4; i++) ui.Frame(); list.Select(9000); Console.WriteLine(list.SelectedItem);
9000Prints 9000, the selected item. This headless example builds the UI tree without opening a window.
Unzip the example, then dotnet run.
The full program
The full example generates inventory data in memory, scrolls to an item, changes its stock and filters the inventory. createRow creates a label for the row pool; bindRow changes its text when that row is reused. version tells the list when an in-place edit needs rebinding. No input files, fonts or GPU are needed for this headless run.
If rowHeight is zero, negative or not in pixels or UI units, the list constructor throws ArgumentException.
using OneGame.UI; using OneGame.UI.Kit; var items = Enumerable.Range(0, 10000) .Select(i => new Item($"Item {i}", 1)).ToList(); var version = 0; var created = 0; var ui = new UiTree(); ui.SetViewport(640, 360); var host = new KitHost(ui); var list = UiKit.VirtualList<Item>(() => items, () => { created++; return Ui.Label(""); }, (row, item, index) => row.Text = $"{item.Name}: {item.Stock}", rowHeight: Length.Ui(28), id: "inventory", version: () => version); ui.Root.Add(Ui.Column(list).Height(Length.Ui(280))); Frame(); Console.WriteLine($"Items: {items.Count}; realized rows: {list.RealizedRowCount}"); list.Select(9000); Frame(); Console.WriteLine($"Selected: {list.SelectedItem!.Name}"); Console.WriteLine($"Rows created after scroll: {created}"); items[9000].Stock = 7; version++; Frame(); Console.WriteLine($"Updated row: {ui.Find("inventory/row/9000")!.Text}"); items = items.Where(item => item.Stock > 1).ToList(); Frame(); Console.WriteLine($"Filtered: {items.Count}; selected index: {list.SelectedIndex}"); void Frame() { for (var i = 0; i < 4; i++) { host.Advance(0); ui.Frame(); } } sealed class Item(string name, int stock) { public string Name { get; } = name; public int Stock { get; set; } = stock; }
dotnet run prints:
Items: 10000; realized rows: 12 Selected: Item 9000 Rows created after scroll: 15 Updated row: Item 9000: 7 Filtered: 1; selected index: 0
Variations
After the snippet at the top, focus the list and send a d-pad step. It prints 9001; selection and scrolling are handled by the list.
ui.Focus(list); ui.Dispatch(UiEvent.Navigate(NavDirection.Down)); Console.WriteLine(list.SelectedIndex);
Related recipes
The short answer
To build a virtualized game inventory list in C#, use UiKit.VirtualList with an item-source lambda, a row factory, a row-binding callback and a fixed row height. UiKit.TextList is its text-only convenience wrapper. Put it in a height-constrained container so the UI has a viewport to scroll through. The full example uses the custom-row API to update stock without rebuilding the inventory screen.
The package is RecursiveAI.OneGame.UI.Kit; the UI guide describes its element tree, layout and list controls. The console output above comes from the actual UI tree, but it is not a rendered game window.
How do I create a virtualized list in C#?
Keep the inventory collection separate from its UI elements. The source lambda returns an IReadOnlyList<T>, while the factory creates an initially empty row element. The binding callback copies the current item into that element. When a row gets reused for a different item, that callback runs again.
The example deliberately jumps to item 9000 rather than creating a separate element for every preceding item. RealizedRowCount reports how many row elements exist. Its first frame sequence reports 12 realized rows for this particular viewport and row height. After the jump, the factory has created 15 rows in total. Those are observations from this layout, not a fixed pool size for every inventory.
All rows must have the same height. Use a fixed slot for icons and keep long descriptions in a separate details panel, rather than expanding an individual row. A table is appropriate when each item has separate name, price and stock columns; its body uses the same list behavior.
How do I update items in a virtualized list?
Return the same collection instance while nothing has changed. Returning a newly allocated list from the source lambda every frame tells the control that the data has changed even when the inventory is identical.
When editing an existing item in place, bump the optional version counter or call Invalidate(). The full example changes stock to 7, increments version and runs the frame sequence again. The realized label then reads Item 9000: 7. That counter is a UI refresh signal; it does not save the inventory or send a gameplay command.
For filtering, replace the source with a new list instance. The selection follows its item to the new index, or clears if the item is gone. The example retains the selected object while filtering out every other item, so its selected index becomes zero. Item equality matters here: ListView<T> uses the default equality comparer to track selection. Choose item identity accordingly when replacing data objects.
How do I scroll an inventory list with a controller?
Focus the list itself, not a recycled row label. Up and Down change selection, while PageUp, PageDown, Home and End support keyboard traversal. Select(index) is also available for opening the inventory at a known item; selection scrolls it into view.
Confirm activates the selected item when you provide an onActivate callback. Selection and activation are different events, so browsing need not spend currency or equip an item. At the first or last item, an unused Up or Down step can move focus to a neighboring control. That lets a controller player leave the list for an equipment or close button rather than becoming stuck in the inventory.