Francisco Aguilera
Francisco Aguilera

Reputation: 3482

Custom CompositeCollection not working

I have tried writing a custom CompositeCollection and CollectionContainer several times, and am just about to give up. Here's what I have. It is seemingly pretty simple.

MainPage.xaml

<phone:PhoneApplicationPage.Resources>
    <vm:MainViewModel x:Key="ViewModel"/>
</phone:PhoneApplicationPage.Resources>

<phone:Panorama DataContext="{StaticResource ViewModel}">        
    <phone:Panorama.ItemsSource>
        <app:CompositeCollection>
            <app:CompositeContainer Collection="{Binding People}"/>
            <models:PersonModel FirstName="John" LastName="Doe"/>
        </app:CompositeCollection>
    </phone:Panorama.ItemsSource>

    <phone:Panorama.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Text="{Binding LastName}"/>
            </StackPanel>
        </DataTemplate>
    </phone:Panorama.ItemTemplate>
</phone:Panorama>

CompositeCollection.cs

namespace PanoramaApp1
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    public class CompositeCollection : ObservableCollection<object>
    {
        Collection<IEnumerable> _collections;

        public CompositeCollection()
            : base()
        {
            _collections = new Collection<IEnumerable>();
        }

        public CompositeCollection(IEnumerable<object> collection)
            : this()
        {
            if (null == collection)
            {
                throw new ArgumentNullException("collection");
            }
            foreach (object obj in collection)
            {
                base.Add(obj);
            }
        }

        public CompositeCollection(List<object> list)
            : this()
        {
            if (null == list)
            {
                throw new ArgumentNullException("list");
            }
            foreach (object obj in list)
            {
                base.Add(obj);
            }
        }

        protected override void ClearItems()
        {
            base.Clear();
            _collections.Clear();
        }

        protected override void InsertItem(int index, object item)
        {
            CompositeContainer container = item as CompositeContainer;
            if (null != container && null != container.Collection)
            {
                InsertContainer(index, container);
            }
            else
            {
                base.InsertItem(index, item);
            }
        }

        private void InsertContainer(int index, CompositeContainer container)
        {
            IEnumerable collection = _collections[index] = container.Collection;
            foreach (object obj in collection)
            {
                base.InsertItem(index++, obj);
            }
        }

        protected override void RemoveItem(int index)
        {
            IEnumerable collection = _collections[index];
            if (null != collection)
            {
                RemoveContainer(index, collection);
            }
            else
            {
                base.RemoveItem(index);
            }
        }

        private void RemoveContainer(int index, IEnumerable collection)
        {
            foreach (object obj in collection)
            {
                base.RemoveItem(index++);
            }
            _collections.RemoveAt(index);
        }

        protected override void SetItem(int index, object item)
        {
            RemoveItem(index);
            InsertItem(index, item);
        }
    }
}

CompositeContainer.cs

namespace PanoramaApp1
{
    using System.Collections;
    using System.Windows;

    public class CompositeContainer : DependencyObject
    {
        public IEnumerable Collection
        {
            get { return (IEnumerable)GetValue(CollectionProperty); }
            set { SetValue(CollectionProperty, value); }
        }

        public static readonly DependencyProperty CollectionProperty =
            DependencyProperty.Register(
            "Collection",
            typeof(IEnumerable),
            typeof(CompositeContainer),
            new PropertyMetadata(null));
    }
}

MainViewModel.cs

using Models;
using System.Collections.ObjectModel;

namespace ViewModels
{
    public class MainViewModel
    {
        public MainViewModel()
        {
            this.People = new ObservableCollection<object>();
            People.Add(new PersonModel("Jane", "Doe"));
            People.Add(new PersonModel("Joe", "Doe"));
            People.Add(new PersonModel("James", "Doe"));
        }

        public ObservableCollection<object> People { get; private set; }
    }
}

PersonModel.cs

using System.ComponentModel;

namespace Models
{
    public class PersonModel : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        private string _firstName;

        private string _lastName;

        public PersonModel(string firstName)
        {
            this.FirstName = firstName;
        }

        public PersonModel(string firstName, string lastName)
            : this(firstName)
        {
            this.LastName = lastName;
        }

        public string FirstName
        {
            get { return _firstName; }
            set
            {
                RaisePropertyChanging("FirstName");
                _firstName = value;
                RaisePropertyChanged("FirstName");
            }
        }

        public string LastName
        {
            get { return _lastName; }
            set
            {
                RaisePropertyChanging("LastName");
                _lastName = value;
                RaisePropertyChanged("LastName");
            }
        }

        private void RaisePropertyChanging(string propertyName)
        {
            if (null != PropertyChanging)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

If I comment out the PersonModel object in the xaml, the application launches, but does not populate the panorama. If I leave it uncommented, I get a very useless exception saying the PersonModel object "couldn't be instantiated". The ItemsSource property of an ItemsControl is of type IEnumerable and it seems like I am enumerating the containers right. Help? Please? lol


Edit: Thanks, the parameterless constructor fixed the first issue. The second issue still remains: it now does populate the second panoramaitem with a PersonModel object, but the first panoramaitem is still empty. Seems like it binded the entire first panoramaitem to the IEnumerable instead of inserting the individual elements.

The designer shows this: i.imgur.com/4fAPe0N.jpg And the emulator shows this: i.imgur.com/UzdyMqk.png i.imgur.com/SWJZ28H.png

Upvotes: 2

Views: 513

Answers (1)

Simon D.
Simon D.

Reputation: 4481

You initialize a PersonModel in the XAML, which will call the default constructor, which is not existing in your code => just add this to PersonModel.cs to solve that part:

public PersonModel() {}

The arguments in the xaml will not be used as constructor arguments, but they will set values using property-setters after your object was created, just like

new PersonModel() { FirstName="John", LastName="Doe" };

Upvotes: 1

Related Questions