I am trying to bind a GridView to a collection, and I want to update GridView after the collection has been set.
The reason I don’t use ObservableCollection is that it will block UI if collection is large(more than 1000 items, and I want to show all items after all items has been added).
In metro style app, there is no BindingList class for C# use, so do I need to implement my own collection class? I tried to implement a class inherited IList and INotifyPropertyChanged.
I did something like this:
<GridView x:Name="ItemsGridView" ItemsSource="{Binding viewCollection}"/>
class MyBindingList<T> : IList<T>, INotifyPropertyChanged
{
private List<Item> _viewCollection = new List<Item>();
public List<Item> viewCollection
{ get { return _viewCollection; } set { _viewCollection = value; } }
public virtual event PropertyChangedEventHandler PropertyChanged;
public void RaiseChanged()
{
this.PropertyChanged(this, new PropertyChangedEventArgs("viewCollection"));
}
}
MyBindingList<Item> list = new MyBindingList<Item>();
ItemsGridView.DataContext = list;
can someone give me a suggestion? Thanks!
On your code, your GridView is binding it’s ItemsSource to viewCollection. The only thing you are missing why it is not notifying is the RaiseChanged() method.
One more thing, I won’t suggest putting the string property on your RaiseChange method. It should be at least accepting a string property name and pass it in the PropertyChangedEventArgs.
An ideal property that notifies/updates the UI Controls in XAML would look like this
So assuming your gonna set your viewCollection somewhere in your ViewModel. It should update the GridView.