我已经尝试过多次编写自定义CompositeCollection和CollectionContainer了,但是我将要放弃。这就是我所拥有的。这似乎很简单。
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对象“无法实例化”。
ItemsSource的ItemsControl属性的类型为IEnumerable,似乎我正在正确枚举容器。
救命?请?大声笑
编辑:谢谢,无参数构造函数修复了第一个问题。第二个问题仍然存在:它现在确实使用PersonModel对象填充了第二个Panoramaitem,但是第一个Panoramaitem仍然为空。好像它将整个第一个Panoramaitem绑定到IEnumerable,而不是插入单个元素。
设计器显示以下内容:i.imgur.com/4fAPe0N.jpg而仿真器显示以下内容:i.imgur.com/UzdyMqk.png i.imgur.com/SWJZ28H.png
最佳答案
您可以在XAML中初始化PersonModel,这将调用默认构造函数,而该默认构造函数在您的代码中不存在=>只需将其添加到PersonModel.cs中即可解决该问题:public PersonModel() {}
xaml中的参数将不会用作构造函数参数,但是在创建对象之后,它们将使用属性设置器设置值,就像
new PersonModel() { FirstName="John", LastName="Doe" };
关于c# - 自定义CompositeCollection不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22681836/