From d772cf496293ababb9675948b16a78a8b3043911 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 14 Apr 2020 14:34:54 +0100 Subject: [PATCH] What it does is that if it doesn't allow multi-selection and you already have a different item selected, select the new one and deselect the previous one. (#361) --- Terminal.Gui/Views/ListView.cs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index 762c002b4..02deb08ad 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -4,7 +4,7 @@ // Authors: // Miguel de Icaza (miguel@gnome.org) // -// +// // TODO: // - Should we support multiple columns, if so, how should that be done? // - Show mark for items that have been marked. @@ -15,8 +15,8 @@ // - Would need a way to specify widths // - Should it automatically extract data out of structs/classes based on public fields/properties? // - It seems that this would be useful just for the "simple" API, not the IListDAtaSource, as that one has full support for it. -// - Should a function be specified that retrieves the individual elements? -// +// - Should a function be specified that retrieves the individual elements? +// using System; using System.Collections; using System.Collections.Generic; @@ -38,6 +38,8 @@ namespace Terminal.Gui { /// This method is invoked to render a specified item, the method should cover the entire provided width. /// /// The render. + /// The list view to render. + /// The console driver to render. /// Describes whether the item being rendered is currently selected by the user. /// The index of the item to render, zero for the first item and so on. /// The column where the rendering will start @@ -69,11 +71,11 @@ namespace Terminal.Gui { /// /// /// The ListView displays lists of data and allows the user to scroll through the data - /// and optionally mark elements of the list (controlled by the AllowsMark property). + /// and optionally mark elements of the list (controlled by the AllowsMark property). /// /// /// The ListView can either render an arbitrary IList object (for example, arrays, List<T> - /// and other collections) which are drawn by drawing the string/ustring contents or the + /// and other collections) which are drawn by drawing the string/ustring contents or the /// result of calling ToString(). Alternatively, you can provide you own IListDataSource /// object that gives you full control of what is rendered. /// @@ -90,7 +92,7 @@ namespace Terminal.Gui { /// /// When AllowsMark is set to true, then the rendering will prefix the list rendering with /// [x] or [ ] and bind the space character to toggle the selection. If you desire a different - /// marking style do not set the property and provide your own custom rendering. + /// marking style do not set the property and provide your own custom rendering. /// /// public class ListView : View { @@ -142,7 +144,7 @@ namespace Terminal.Gui { bool allowsMarking; /// - /// Gets or sets a value indicating whether this allows items to be marked. + /// Gets or sets a value indicating whether this allows items to be marked. /// /// true if allows marking elements of the list; otherwise, false. /// @@ -159,6 +161,9 @@ namespace Terminal.Gui { } } + /// + /// If set to true allows more than one item to be selected. If false only allow one item selected. + /// public bool AllowsMultipleSelection { get; set; } = true; /// @@ -179,7 +184,7 @@ namespace Terminal.Gui { } /// - /// Gets or sets the currently selecteded item. + /// Gets or sets the currently selected item. /// /// The selected item. public int SelectedItem { @@ -330,8 +335,10 @@ namespace Terminal.Gui { return false; if (!AllowsMultipleSelection) { for (int i = 0; i < Source.Count; i++) { - if (Source.IsMarked (i) && i != selected) - return false; + if (Source.IsMarked (i) && i != selected) { + Source.SetMark (i, false); + return true; + } } } return true;