自定义复合集合不工作
本文关键字:工作 集合 复合 自定义 | 更新日期: 2023-09-27 17:54:25
我已经尝试过多次编写自定义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对象"无法实例化"。ItemsControl的ItemsSource属性的类型是IEnumerable,似乎我枚举容器的权利。帮助吗?好吗?lol
编辑:谢谢,无参数构造函数修复了第一个问题。第二个问题仍然存在:它现在确实用PersonModel对象填充第二个全景项,但第一个全景项仍然是空的。似乎它将整个第一个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" };