-
Notifications
You must be signed in to change notification settings - Fork 782
Fixes #4869. Add ListView<T> with typed Value, SelectedItem, Index #4870
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
Merged
tig
merged 12 commits into
tui-cs:v2_develop
from
YourRobotOverlord:feature/generic-listview
Mar 30, 2026
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
53fb200
Add generic ListView<T> with typed Value, SelectedItem, and Index pro…
YourRobotOverlord 05b70fb
Add GenericListView UICatalog scenario demonstrating ListView<T>
YourRobotOverlord 70b5b93
Add AspectGetter delegate to ListWrapper<T> and ListView<T>
YourRobotOverlord c5188e2
Improve IValue.GetValue() XML doc in ListView<T>
YourRobotOverlord 5fadf8d
Update Examples/UICatalog/Scenarios/GenericListView.cs
YourRobotOverlord 95a68ec
Update Examples/UICatalog/Scenarios/GenericListView.cs
YourRobotOverlord ecb82b0
Update Examples/UICatalog/Scenarios/GenericListView.cs
YourRobotOverlord 5cc6b86
Update Examples/UICatalog/Scenarios/GenericListView.cs
YourRobotOverlord 9fda182
Address PR feedback: remove AspectGetter/ColorGetter from library
YourRobotOverlord 38b14de
Revert cosmetic ListWrapper<T> changes, keep GetColumns() fix
YourRobotOverlord a4c3f9e
code formatting
YourRobotOverlord 5759f37
Clarify ListView<T>.Value setter behavior in XML docs
YourRobotOverlord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| #nullable enable | ||
| using System.Collections.ObjectModel; | ||
| using System.Text; | ||
|
|
||
| namespace UICatalog.Scenarios; | ||
|
|
||
| [ScenarioMetadata ("Generic ListView<T>", "Demonstrates ListView<T> with typed Value, SelectedItem, and Index")] | ||
| [ScenarioCategory ("Controls")] | ||
| [ScenarioCategory ("ListView")] | ||
| public class GenericListView : Scenario | ||
| { | ||
| private ListView<Country>? _listView; | ||
| private ObservableCollection<string> _eventList = []; | ||
| private ListView? _eventListView; | ||
| private Label? _nameLabel; | ||
| private Label? _capitalLabel; | ||
| private Label? _populationLabel; | ||
| private Label? _indexLabel; | ||
| private CheckBox? _cancelNextCb; | ||
| private bool _cancelNext; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Main () | ||
| { | ||
| ConfigurationManager.Enable (ConfigLocations.All); | ||
| using IApplication app = Application.Create (); | ||
| app.Init (); | ||
|
|
||
| using Window appWindow = new () { Title = GetQuitKeyAndName () }; | ||
|
|
||
| ObservableCollection<Country> countries = | ||
| [ | ||
| new ("Australia", "Canberra", 26_000_000), | ||
| new ("Brazil", "Brasília", 215_000_000), | ||
| new ("Canada", "Ottawa", 38_000_000), | ||
| new ("Denmark", "Copenhagen", 5_900_000), | ||
| new ("Egypt", "Cairo", 104_000_000), | ||
| new ("France", "Paris", 68_000_000), | ||
| new ("Germany", "Berlin", 84_000_000), | ||
| new ("Hungary", "Budapest", 9_700_000), | ||
| new ("India", "New Delhi", 1_428_000_000), | ||
| new ("Japan", "Tokyo", 124_000_000) | ||
| ]; | ||
|
|
||
| // -- Cancel checkbox -------------------------------------------------- | ||
| _cancelNextCb = new CheckBox | ||
| { | ||
| X = 0, | ||
| Y = 0, | ||
| Text = "C_ancel next selection change" | ||
| }; | ||
| _cancelNextCb.ValueChanging += (_, args) => _cancelNext = args.NewValue == CheckState.Checked; | ||
| appWindow.Add (_cancelNextCb); | ||
|
|
||
| // -- ListView<Country> ------------------------------------------------ | ||
| _listView = new ListView<Country> | ||
| { | ||
| Title = "_Countries", | ||
| X = 0, | ||
| Y = Pos.Bottom (_cancelNextCb) + 1, | ||
| Width = 22, | ||
| Height = Dim.Fill (4), | ||
| BorderStyle = LineStyle.Single, | ||
| AspectGetter = c => c.Name | ||
| }; | ||
| _listView.SetSource (countries); | ||
| appWindow.Add (_listView); | ||
|
|
||
| // -- Detail panel ----------------------------------------------------- | ||
| FrameView detailPanel = new () | ||
| { | ||
| Title = "_Selected", | ||
| X = Pos.Right (_listView) + 1, | ||
| Y = Pos.Top (_listView), | ||
| Width = Dim.Fill (), | ||
| Height = _listView.Height | ||
| }; | ||
| appWindow.Add (detailPanel); | ||
|
|
||
| Label nameTitleLbl = new () { X = 1, Y = 1, Text = "Name: " }; | ||
| detailPanel.Add (nameTitleLbl); | ||
| _nameLabel = new Label { X = Pos.Right (nameTitleLbl), Y = 1, Width = Dim.Fill (1), Text = "(none)" }; | ||
| detailPanel.Add (_nameLabel); | ||
|
|
||
| Label capitalTitleLbl = new () { X = 1, Y = 2, Text = "Capital: " }; | ||
| detailPanel.Add (capitalTitleLbl); | ||
| _capitalLabel = new Label { X = Pos.Right (capitalTitleLbl), Y = 2, Width = Dim.Fill (1), Text = "" }; | ||
|
YourRobotOverlord marked this conversation as resolved.
Outdated
|
||
| detailPanel.Add (_capitalLabel); | ||
|
|
||
| Label populationTitleLbl = new () { X = 1, Y = 3, Text = "Population:" }; | ||
| detailPanel.Add (populationTitleLbl); | ||
| _populationLabel = new Label { X = Pos.Right (populationTitleLbl), Y = 3, Width = Dim.Fill (1), Text = "" }; | ||
|
YourRobotOverlord marked this conversation as resolved.
Outdated
|
||
| detailPanel.Add (_populationLabel); | ||
|
|
||
| Label indexTitleLbl = new () { X = 1, Y = 5, Text = "Index: " }; | ||
| detailPanel.Add (indexTitleLbl); | ||
| _indexLabel = new Label { X = Pos.Right (indexTitleLbl), Y = 5, Width = Dim.Fill (1), Text = "" }; | ||
|
YourRobotOverlord marked this conversation as resolved.
Outdated
|
||
| detailPanel.Add (_indexLabel); | ||
|
|
||
| // -- Event log -------------------------------------------------------- | ||
| _eventList = []; | ||
| _eventListView = new ListView | ||
| { | ||
| Title = "_Events", | ||
| X = 0, | ||
| Y = Pos.Bottom (_listView) + 1, | ||
| Width = Dim.Fill (), | ||
| Height = Dim.Fill (), | ||
| Source = new ListWrapper<string> (_eventList), | ||
| BorderStyle = LineStyle.Single | ||
| }; | ||
| appWindow.Add (_eventListView); | ||
|
|
||
| // -- Wire events ------------------------------------------------------ | ||
| _listView.ValueChanging += OnValueChanging; | ||
| _listView.ValueChanged += OnValueChanged; | ||
|
|
||
| app.Run (appWindow); | ||
| } | ||
|
|
||
| private void OnValueChanging (object? sender, ValueChangingEventArgs<Country?> args) | ||
| { | ||
| if (_cancelNext) | ||
| { | ||
| args.Handled = true; | ||
| _cancelNext = false; | ||
|
|
||
| if (_cancelNextCb is not null) | ||
| { | ||
| _cancelNextCb.Value = CheckState.UnChecked; | ||
| } | ||
|
|
||
| LogEvent ($"ValueChanging CANCELLED: {FormatCountry (args.CurrentValue)} -> {FormatCountry (args.NewValue)}"); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| LogEvent ($"ValueChanging: {FormatCountry (args.CurrentValue)} -> {FormatCountry (args.NewValue)}"); | ||
| } | ||
|
|
||
| private void OnValueChanged (object? sender, ValueChangedEventArgs<Country?> args) | ||
| { | ||
| UpdateDetail (args.NewValue); | ||
| LogEvent ($"ValueChanged: {FormatCountry (args.OldValue)} -> {FormatCountry (args.NewValue)}"); | ||
| } | ||
|
|
||
| private void UpdateDetail (Country? country) | ||
| { | ||
| if (_nameLabel is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (country is null) | ||
| { | ||
| _nameLabel.Text = "(none)"; | ||
| _capitalLabel!.Text = ""; | ||
| _populationLabel!.Text = ""; | ||
| _indexLabel!.Text = ""; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| _nameLabel.Text = country.Name; | ||
| _capitalLabel!.Text = country.Capital; | ||
| _populationLabel!.Text = $"{country.Population:N0}"; | ||
| _indexLabel!.Text = _listView?.Index?.ToString () ?? ""; | ||
| } | ||
|
|
||
| private void LogEvent (string message) | ||
| { | ||
| _eventList.Add (message); | ||
|
|
||
| if (_eventListView is not null) | ||
| { | ||
| _eventListView.MoveEnd (); | ||
| } | ||
| } | ||
|
|
||
| private static string FormatCountry (Country? c) => c is null ? "null" : c.Name; | ||
| } | ||
|
|
||
| /// <summary>A simple record used to demonstrate <see cref="ListView{T}"/>.</summary> | ||
| internal record Country (string Name, string Capital, int Population); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.