Reputation: 8552
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
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
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
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
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
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
Upvotes: 4