question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ComboBox source is IList but can only be list of strings

See original GitHub issue

If ComboBox source is set as list of objects, e.g.:


public class CategoryItem
{
	public CategoryItem(int id, string name)
	{
		Id = id;
		Name = name;
	}
	public int Id { get; set; }
	public string Name { get; set; }

	public override string ToString()
	{
		return Name;
	}
}

var categoryComboBox = new ListView() {
	X = 1,
	Y = 1,
	Width = Dim.Fill (),
	Height = Dim.Fill (2),
};

categoryComboBox.SetSource (new List<CategoryItem> () {
	new CategoryItem (1, "Category"),
	new CategoryItem (2, "Other category")
});

Win.Add (categoryComboBox);

it’s throwing exception when searching, becuse of a string cast on this line: https://github.com/migueldeicaza/gui.cs/blob/527c8aa36526e0e0347cecae7ecd66196ee5aae4/Terminal.Gui/Views/ComboBox.cs#L327

I understand that it’s due to a fact that ComboBox item can be any string value, not like in ListView where you can select values only from DataSource.

My use case is that I want to choose from predefined set of values with search capability. Is it something ComboBox should do or it’s rather an idea for a new control like DropDownList?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fergusonrcommented, Jul 5, 2020

Hi, SelectedItem has now been implemented in PR #710. Its read-only. It does not make any sense to set it unless the full source list is displayed. Something to consider in the future.

0reactions
tigcommented, Jul 8, 2020

Sweet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to properly bind an IList to a Combobox?
I've tried adding "genreComboBox.DataContext = genres" aswell but this will end up in the combobox getting filled with empty strings. Any help ...
Read more >
Solved: Will a comboBox accept a List as its DataSource
So I created a List Class that has two private fields (one is a string and the other an integer). I populated the...
Read more >
C# method listbox or combobox as parameter
Hi all, First time at MS Q&A I am using VS2015 and C# I have code that loads data into a listbox. No...
Read more >
ComboBox.ObjectCollection.IList.Add(Object) Method
It can be used only when the ComboBox.ObjectCollection instance is cast to an IList interface. This method adds an item to the combo...
Read more >
Programmatically Binding DataSource To ComboBox In ...
In this article, we will learn how to bind ComboBox DataSource to Array of objects, List of Objects, DataTable, DataSet, DataView and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found