Arg, although I was googling, I really would appreciate it if someone could break my problem, since all the code examples on the Internet confuse me more than they help (maybe it's only too late) ...
I have a simple class as defined below:
public class Person { int _id; string _name; public Person() { } public int ID { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } }
which is stored in the database, and after a bit more code, I put it in the ObservableCollection object to try to bind the data in WPF later:
public class People : ObservableCollection<Person> { public People() : base() { } public void Add(List<Person> pListOfPeople) { foreach (Person p in pListOfPeople) this.Add(p); } }
In XAML, I have a ListView that I would like to populate with a ListViewItem (consisting of a text block) for each element of the People object as it is being updated from the database. I would also like this text block to bind to the Name property of the Person object.
At first I thought I could do this:
lstPeople.DataContext = objPeople;
where lstPeople is my ListView control in my XAML, but that of course does nothing. I found TONS of examples online where people through XAML create an object and then bind it through their XAML; but not where we attach to the instantiated object and rewrite accordingly.
Can someone please give me some pointers to:
A) How do I link a ListView control to my People object instance?
B) How can I apply a template to my ListView to format it for objects in the collection?
Even links to a decent example (not one working on an object declared in XAML, please) will be appreciated.
Thank you for your time.
collections data-binding wpf
Randster
source share