I'm sure there should be an easier way to do this. I have a class based on Collection, and that class is a collection of another class. Currently, whenever I have a new item in my class, I assign that item to the listbox. I can't seem to figure out a way to assign all of the values in the collection class, because it is a collection, to the collection of the listbox. Any ideas? Thanks
Ok, what I've done so far is I have a tostring override in the Class used in the collection. This is what I want the listbox to show.
public override string ToString()
{
return string.Format("{0} {1}: {2}", tTypev.ToString(),
Datev.ToString("MM/dd/yyyy"), Amountv.ToString("C"));
}
Which is what I want each item in the listbox to show.
class Transactions : System.Collections.CollectionBase
{
...
}
Is my collections class, containing a collection of the other class, Tansaction. Curently, I use the lstTransactions.Items.Add(), .Remove, .RemovAt, etc to add items to the list box, and the .Add(), .Remove, etc to add items to the Collection Class, Transactions. But I'm trying to decrease reliance on outside controls, and only use them in a few lines of code. I was trying to use something like:
lstTransactions.DataSource = (Transaction)myTrans;
but that didn't seem to work. Mainly because I couldn't figure out what property DataSource took.
I also tried:
lstTransactions.Items =
but it told me that items was read only.
In Windows Form:
Sample Code: In the below sample the output list box would display 2 items : Apple and Ball.
In WPF:
You could use ItemSource property of ListBox to bind a collection (which may be a List, Enumerable, Collections.._ do the job
Sample snippet: