Allow user to reorder list? #1490
Replies: 6 comments
-
Of course you can. Just sort the source list the way you want and set the list source again. |
Beta Was this translation helpful? Give feedback.
-
Here is an example Application.Init ();
var lv = new ListView (new List<string> { "dog", "cat", "horse", "catipillar" }) {
Width = 10,
Height = 10
};
var btnAsc = new Button (0, 11, "Asc");
btnAsc.Clicked += () => {
lv.SetSource(lv.Source.ToList ().Cast<string> ().OrderBy (a => a).ToList ());
};
var btnDesc = new Button (0, 12, "Desc");
btnDesc.Clicked += () => {
lv.SetSource (lv.Source.ToList ().Cast<string> ().OrderByDescending (a => a).ToList ());
};
var win = new Window ();
win.Add (lv);
win.Add (btnAsc);
win.Add (btnDesc);
Application.Run (win);
Application.Shutdown (); |
Beta Was this translation helpful? Give feedback.
-
What im trying to achieve is allowing the user to move a specific item up or down the list, exampe we have the items A, B, C. Bonus if the item chages background color while selected. |
Beta Was this translation helpful? Give feedback.
-
See here: In the |
Beta Was this translation helpful? Give feedback.
-
Here is a simple demo: Application.Init ();
var lv = new ListView (new List<string> { "A", "B", "C" }) {
Width = 10,
Height = 10
};
var btnUp = new Button (0, 11, "^");
btnUp.Clicked += () => {
if (lv.SelectedItem > 0) {
var item = lv.Source.ToList () [lv.SelectedItem];
lv.Source.ToList () [lv.SelectedItem] = lv.Source.ToList () [lv.SelectedItem - 1];
lv.Source.ToList () [lv.SelectedItem - 1] = item;
lv.SelectedItem--;
lv.SetChildNeedsDisplay ();
}
};
var btnDown = new Button (0, 12, "v");
btnDown.Clicked += () => {
if (lv.SelectedItem < lv.Source.Count - 1) {
var item = lv.Source.ToList () [lv.SelectedItem];
lv.Source.ToList () [lv.SelectedItem] = lv.Source.ToList () [lv.SelectedItem + 1];
lv.Source.ToList () [lv.SelectedItem + 1] = item;
lv.SelectedItem++;
lv.SetChildNeedsDisplay ();
}
};
var win = new Window ();
win.Add (lv);
win.Add (btnUp);
win.Add (btnDown);
Application.Run (win);
Application.Shutdown (); |
Beta Was this translation helpful? Give feedback.
-
Thank you that should do fine! |
Beta Was this translation helpful? Give feedback.
-
Is there someway to use this framework where i can provide a list of items and allow the user to change the order of them?
Beta Was this translation helpful? Give feedback.
All reactions