0

I am new to programming for winrt. I am using VS 2015, trying to do some testing on ObservableCollection to see how the change in the collection got reflected on the UI. I must have done something incorrectly. Just do not know what.

This is my model:

class MyItems
{
    public int ItemID { get; set; }
    public string ItemDescription { get; set; }
}
class MyItemList:List<MyItems>
{
    public MyItemList()
    {
        Random r = new Random(DateTime.Now.Day);
        for (int i = 0; i < r.Next(10)+1; i++)
        {
            this.Add(new MyItems() { ItemID = i + 1,
                ItemDescription = string.Format("Item_{0}", i + 1) });
        }
    }
    public ObservableCollection<MyItems> getEven()
    {
        return new ObservableCollection<MyItems>(this.Where(x=>x.ItemID%2==0).ToList());
    }

    public void AddMoreItems(int v)
    {
        int total = this.Count;
        for (int i = 0  ; i < v; i++)
        {
            this.Add(new MyItems() { ItemID = total + i, ItemDescription = string.Format("Item_{0}", total+i) });
        }
    }
}

On MainPage.xaml, I have a button to add items to the list. I have created a listview programmatically and binding to the dataset two ways.

public sealed partial class MainPage : Page
{
    static MyItemList myItems = new MyItemList();
    public MainPage()
    {
        this.InitializeComponent();
        var t = myItems.getEven();
        ListView myListView = new ListView() { ItemTemplate = (DataTemplate)Resources["myItemTemplate"] };
        myListView.DataContext = t;
        var binding = new Binding();
        binding.Source = t;
        binding.Mode = BindingMode.TwoWay;
        myListView.SetBinding(ListView.ItemsSourceProperty, binding);
        MyGrid.Children.Add(myListView);
    }

    private void AddItems_Click(object sender, RoutedEventArgs e)
    {
        myItems.AddMoreItems(3);
    }
}

When I clicked on the button, 3 more items are added but they are not reflected in my listview. Something else needs to be done beside using the ObservableCollection and set binding to twoWays?

user1205746
  • 3,110
  • 11
  • 44
  • 73

1 Answers1

1

Your problem, essentially, is here:

public ObservableCollection<MyItems> getEven()
{
    return new ObservableCollection<MyItems>(this.Where(x=>x.ItemID%2==0).ToList());
}

When you query this and call ToList(), you are creating a new list that is independent of the original. If you add items to the original, it's not going to be reflected in your derived list. Think of it this way:

public ObservableCollection<MyItems> getEven()
{
    var filteredList = this.Where(x=>x.ItemID%2==0).ToList()
    return new ObservableCollection<MyItems>(filteredList);
}

Adding to this is not going to change the contents filteredList at all.

Additionally, creating a new ObservableCollection every time you access getEven instead of modifying an existing one means that the events when adding and deleting to the observable collection will never fire.

You are using observable collections in a fundamentally incorrect way. Why not just have MyItemList derive from ObservableCollection<T> instead of List<T>?

Collection that inherits from ObservableCollection - What are the benefits?

Also, if you're trying to filter by even/odd, you should look into ICollectionView

WPF Binding filtered ObservableCollection ICollectionView to Combobox

Community
  • 1
  • 1
Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
  • Thanks Paul for the information. I am new to programming on WinRT, so I may have misused the ObservableCollection. 2 follow up questions: First, If I derive MyItemList from ObservableCollection, will my data filtering set from getEven gets updated if items are added to the parent set MyItemList, or do I still have to use ICollectionView for that? Second, will the filtering set get updated if I only use ICollectionView (without deriving MyItemList from ObservableCollection) when items are added to MyItemList? Thanks again! – user1205746 Apr 18 '16 at 14:00
  • Don't bother... I think I got it after reading your explanation more carefully. Just do not know my way around with the new notion of ObservableCollection in the first place, your explanation says it all.. a new list was created and that list is not an observablecollection. Got it! – user1205746 Apr 18 '16 at 17:56
  • I now seem to understand your explanation but something is still bothering me. I understand the ObservableCollection in getEven will not be affected if changes are made to the original MyListItem. But is there a way for me to notify getEven and trigger the change to the ObservableCollection set in getEven? Does that make sense to you? – user1205746 Apr 18 '16 at 18:32