Mostafiz Rahman
Mostafiz Rahman

Reputation: 8552

Update or refresh Listview in Windows Phone 8.1 app with C# and XAML

I need to refresh a ListView when the ItemsSource are updated. I've declared the myListView as a ListView within the XAML file and assigned the myList as the itemssource in the C# code with following code snippet:

myListView.ItemsSource = myList;

Now, my question is, how can I refresh myListView?

Upvotes: 2

Views: 2241

Answers (5)

Kshitij Jhangra
Kshitij Jhangra

Reputation: 607

i'd created a test app - krayknot to test this scenario and here is the code:

Include INotifyPropertyChanged

public sealed partial class PivotPage : Page, INotifyPropertyChanged

create eventhandler

public event PropertyChangedEventHandler PropertyChanged;

Create ObservableCollection

ObservableCollection<Feeds> oc = new ObservableCollection<Feeds>();

and here is the method,

private async void BindFeeds(string URL)
        {
            progressRing.IsActive = true;

            var uri = new Uri(URL);
            var httpClient = new HttpClient();

            // Always catch network exceptions for async methods
            httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
            var result = await httpClient.GetAsync(uri);

            var xmlStream = await result.Content.ReadAsStreamAsync();
            XDocument loadedData = XDocument.Load(xmlStream);

            foreach (var data in loadedData.Descendants("Table"))
            {


                try
                {
                    oc.Add(new Feeds
                    {
                        BlogId = data.Element("Blogs_CID").Value.ToString(),
                        Description = data.Element("Blogs_BriefDescription").Value.ToString(),
                        IconImage = videoIcon,
                        Heading = data.Element("Blogs_Heading").Value.ToString().Trim(),
                        MainImage = feedImageThumbnail, //data.Element("Blogs_SquareImagePath").Value.ToString(),
                        TimeStamp = data.Element("TimeElapsed").Value.ToString(),
                        SourceURL = data.Element("Blogs_SourceURL").Value.ToString()
                    });

                }
                catch (Exception)
                {
                    //throw;
                }               
            }
            httpClient.Dispose();

            if (lstLatest.Items.Count == 0)
            {
                lstLatest.ItemsSource = oc;
            }
            else
            {
                NotifyPropertyChanged();
            }

            httpClient.Dispose();
            progressRing.IsActive = false;
        }

Upvotes: 0

Cloy
Cloy

Reputation: 2181

Try this

  if (ListView.Items.Count > 0)
  {
     ListView.ItemsSource = null;
  }
  listItem.Clear();

Just assigning the new list view objects does not clear the data. You need to clear the ItemSource and also clear the list array used to bind the list view

Upvotes: 0

Ryan G
Ryan G

Reputation: 71

Great to see that it works only by the use of System.Collections.ObjectModel.ObservableCollection, then set the collection in the ListView.ItemsSource.

Upvotes: 0

Ali
Ali

Reputation: 617

You need to use and ObservableCollection

public ObservableCollection<string> MyList { get; set; }

After setting the observable collection as your ItemsSource every change to the collection will automatically be reflected on your listview.

Upvotes: 2

Burak Kaan K&#246;se
Burak Kaan K&#246;se

Reputation: 831

What do you mean by refreshing ? If you want your UI to be refreshed , your myList must be ObservableCollection type and your class must implement INotifyPropertyChanged interface.

Check out this post

https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx

Upvotes: 4

Related Questions