Matthias Herrmann Matthias Herrmann - 11 months ago 72
C# Question

Listbox Resource Windows 10 universal

i wanted to test the listbox xaml item.
I created a Listbox and a .cs Class called "Mitarbeiter".

using System.Collections.Generic;

public class Mitarbeiter

public int Age { get; set; }
public string Name { get; set; }
List<Mitarbeiter> berater = new List<Mitarbeiter>
new Mitarbeiter(13,"name1"),
new Mitarbeiter(18,"name2")

public Mitarbeiter(int inputage, string inputname)
Age = inputage;
inputname = Name;


Now i want to bind my listbox to the objects of the "berater" list and it should display the two "mitarbeiter" objects in the list.
Im a beginner and dont have so much experience so every code you write in your answer is appreciated.

Answer Source

In the XAML-file you have a ListBox

<ListBox x:Name="myListBox" />

and in the cs-file you write

Mitarbeiter m = new Mitarbeiter();
myListBox.ItemsSource = m.berater;

In the Mitarbeiter-class override the ToString(), to display the name of the Mitarbeiter in the listbox.

public override String ToString()
    return Name;

Here is a good tutorial: