A
A
Anton2016-01-13 15:47:01
WPF
Anton, 2016-01-13 15:47:01

How to update a ListBox (WPF) on demand?

Hello!
There is a ListBox which is filled with the data from a DB. Further, if the record was edited in the application, this list must be updated (rebuild it). How to implement it?
Now I'm building a list in a very "simple" way:

var lines = new List<NewsList>();

// Заполняем
// lines.Add

myListBox.DataContext = lines;

I understand that this is the easiest option, which is not ready for any update. And how would it be more competent to implement the same list, but only called (formed) on demand? For example, the first time when the application is initialized, the second time after submitting the form.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Pavlov, 2016-01-13
@lexxpavlov

First, it's better to use an ItemsSource rather than a DataContext. And secondly, instead of List, take ObservableCollection.

M
MrDywar Pichugin, 2016-01-14
@Dywar

+ to what Maxim Vanyushkin wrote Maxim Vanyushkin :D
To catch changes in the objects themselves inside the collection, you need to use BindingList .
And learn WPF MVVM, spend a couple of hours.
Developing Using the Design Pattern...

L
Larry Underwood, 2016-01-14
@Hydro

+ to what Aleksey Pavlov wrote :
If an already existing record is being edited, then the record object must implement INotifyPropertyChanged, then changing this object in any part of the program will cause it to be updated in all places of the UI where it is shown.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question