自定义CompositeCollection不起作用

时间:2014-03-27 08:21:15

标签: c# .net wpf windows-phone-8

我尝试多次编写自定义CompositeCollectionCollectionContainer,而我正要放弃。这就是我所拥有的。它看似很简单。

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));
            }
        }
    }
}

如果我在xaml中注释掉PersonModel对象,则应用程序将启动,但不会填充全景图。如果我将其取消注释,我会得到一个非常无用的异常,说PersonModel对象“无法实例化”。 ItemsSourceItemsControl属性类型为IEnumerable,似乎我正在枚举容器。 救命?请?洛尔


编辑:谢谢,无参数构造函数修复了第一个问题。第二个问题仍然存在:它现在用PersonModel对象填充第二个geopitem,但第一个geopitem仍然是空的。似乎它将整个第一个geopitem绑定到IEnumerable而不是插入单个元素。

设计师会显示:i.imgur.com/4fAPe0N.jpg并且模拟器会显示:i.imgur.com/UzdyMqk.png i.imgur.com/SWJZ28H.png

1 个答案:

答案 0 :(得分:1)

您在XAML中初始化PersonModel,它将调用默认构造函数,该构造函数在您的代码中不存在=&gt;只需将其添加到PersonModel.cs即可解决该部分:

public PersonModel() {}

xaml中的参数不会用作构造函数参数,但是在创建对象后它们将使用property-setters设置值,就像

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