Is there a way to use an index value on ComboBox/ListView? #1511
Answered
by
BDisp
Ben2146053
asked this question in
Q&A
-
|
Is there any way to set an index value (like a guid) for each of rows in a listView or combobox but that the index value is hidden from the display? |
Beta Was this translation helpful? Give feedback.
Answered by
BDisp
Nov 12, 2021
Replies: 1 comment
-
|
Yes, is possible. But be aware that you have to always binding the actual selected item to the guid. To display custom data you must implement a class for that and overriding the Application.Init ();
var t = Application.Top;
List<Product> products = new List<Product> ();
products.Add (new Product (0, Guid.NewGuid (), "Laptop"));
products.Add (new Product (0, Guid.NewGuid (), "Desktop"));
products.Add (new Product (0, Guid.NewGuid (), "Printer"));
var lv = new ListView (products) {
Width = Dim.Percent (80),
Height = Dim.Fill (2)
};
var lbl = new Label () {
Y = Pos.Bottom (lv) + 1,
Width = Dim.Percent (80),
Height = 1
};
lv.SelectedItemChanged += (e) => {
lbl.Text = $"{products [e.Item].Key} - {products [e.Item].Name}";
};
t.Add (lv, lbl);
Application.Run ();
Application.Shutdown ();
class Product {
public int Index { get; set; }
public Guid Key { get; set; }
public string Name { get; set; }
public Product (int index, Guid guid, string name)
{
Index = index;
Key = guid;
Name = name;
}
public override string ToString ()
{
return Name;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Ben2146053
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, is possible. But be aware that you have to always binding the actual selected item to the guid. To display custom data you must implement a class for that and overriding the
ToStringmethod. You can add buttons on the right side to Add, Remove and Edit to manipulate items.