1

I am trying to load a listview of mine with the data from my database. And the data will be update periodically.

<ListView Margin="10" Name="lvDataBinding" ></ListView> //frontend

I am now experimenting on using backgroundworker to load the new data but no luck on it

     <ListView Margin="10" Name="lvDataBinding" ></ListView> //frontend

    ObservableCollection<string> mNames = new ObservableCollection<string>(); 


public SettingsControl()
            {
                InitializeComponent();

                listview1.ItemsSource = mNames;
                mNames.Add("1");
                var mWorker = new BackgroundWorker();
                mWorker.DoWork += Worker_ListenForNewSMS;
                mWorker.RunWorkerAsync();


        }

    private void Worker_ListenForNewSMS(object sender, DoWorkEventArgs e)
            {
                while (true)
                {
                    Thread.Sleep(2000);
                    //fetch and load new data here, cant seems to work
                   listview1.ItemsSource = "NEWDATA";


                }
            }

So how could it reload the data async, i do not wan to refresh the page.

I need it to be in a loop so that i could listen to the ddatabase and if there is new entry, it will update the list

I am aware of data blinding but i am very little experience doing that, can anyone provide a example to guide me for that.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

3 Answers3

0

Since, the ItemsSource of ListView is already configured so best way to refresh the data using below code:

mNames.Clear();
// Now add the ite,
mNames.Add("item1");
mNames.Add("item2");

Additional Thogught

You an implement AddRange method on ObservableCollection so above multiple statments can be converted to mNames.AddRange(<Collection Object>). Follow this link to implement AddRange.

// Alternative Solution

If you tries to update the ItemsSource then you should refresh the View manually. Lets assume that you need to update it ONE time after ALL the elements were added to the ItemsSource) so you should use this approach:

// Create the view.
ICollectionView view = CollectionViewSource.GetDefaultView(mNames);

// To Refresh calls like this.
view.Refresh();

I would prefer with option 1.

user1672994
  • 10,509
  • 1
  • 19
  • 32
0

Add the new data to the source collection using the dispatcher:

private void Worker_ListenForNewSMS(object sender, DoWorkEventArgs e)
{
    while (true)
    {
        Thread.Sleep(2000);
        Dispatcher.BeginInvoke(new Action(() =>
        {
            mNames.Add("NEWDATA");
        }));
    }
}

The dispatcher marshals the call back to the UI thread. This is required since you can only update the data-bound collection on the UI thread (unless you use the BindingOperations.EnableCollectionSynchronization method).

You also need to use the dispatcher if you for some reason are re-setting the ItemsSource property:

private void Worker_ListenForNewSMS(object sender, DoWorkEventArgs e)
{
    while (true)
    {
        Thread.Sleep(2000);
        Dispatcher.BeginInvoke(new Action(() =>
        {
            listview1.ItemsSource = new List<string> { "NEWDATA" };
        }));
    }
}

You cannot set the property of a UI control from a background thread.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

In the end i load the listview with a observableCollection

and uses a button to trigger the refresh function