首先我会解释我的问题,然后我问......
我有三个实体,所有者、保护者和动物......动物可以有一个所有者或一个保护者,到目前为止,很好......
但是,当我将它分组到 ListView 中时,如果我将PropertyGroupDescription设置为 Owner.Name,如果动物有一个 Protector 作为它的所有者,那么在 ListView 中这个组将是空白的。
我的实体:
public partial class Animal : BaseModel
{
public int Id { get; set; }
public string Name { get; set; }
public virtual AnimalSpecie AnimalSpecie { get; set; }
public virtual AnimalBreed AnimalBreed { get; set; }
public DateTime DateBirth { get; set; }
public Gender Gender { get; set; }
public decimal Weight { get; set; }
public bool Vaccinated { get; set; }
public bool Castrated { get; set; }
public virtual Owner Owner { get; set; }
public virtual Protector Protector { get; set; }
public DateTime InsertDate { get; set; }
public DateTime UpdateDate { get; set; }
public Animal()
{
this.Owner = new Owner();
this.Protector = new Protector();
}
}
public partial class BaseOwner : BaseModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string FormatedPhone
{
get
{
if (this.Phone == null)
return string.Empty;
switch (this.Phone.Length)
{
case 11:
return Regex.Replace(this.Phone, @"(\d{2})(\d{4})(\d{4})", "($1) $2-$3");
case 12:
return Regex.Replace(this.Phone, @"(\d{2})(\d{5})(\d{4})", "($1) $2-$3");
default:
return this.Phone;
}
}
}
public string CellPhone { get; set; }
public string FormatedCellPhone
{
get
{
if (this.CellPhone == null)
return string.Empty;
switch (this.CellPhone.Length)
{
case 11:
return Regex.Replace(this.Phone, @"(\d{2})(\d{4})(\d{4})", "($1) $2-$3");
case 12:
return Regex.Replace(this.Phone, @"(\d{2})(\d{5})(\d{4})", "($1) $2-$3");
default:
return this.CellPhone;
}
}
}
public string Email { get; set; }
public virtual ICollection<Animal> Animals { get; set; }
}
Owner 和 Protectors 派生自 BaseOwner。
如何在组描述中显示所有者的姓名或保护者的姓名?
Ps.:首先,对不起我的英语,如果不清楚,我会尝试重新表述。
Ps.2:我正在使用 MVVM。
更新
我确实在这里取得了一些进展:
ListView ListViewAnimal = (ListView)this.AnimalView.FindName("ListViewAnimal");
this._AnimalsOriginal = new Repository.Animal().GetAllCompleted();
this.Animals = new Repository.Animal().GetAllCompleted();
if (this.ListViewItems == null)
{
this.ListViewItems = new CollectionViewSource { Source = this.Animals };
}
foreach (var animal in this.Animals)
{
if (animal.Owner.Id > 0)
{
this.ListViewItems.GroupDescriptions.Add(new PropertyGroupDescription("Owner.Name"));
}
else
{
this.ListViewItems.GroupDescriptions.Add(new PropertyGroupDescription("Protector.Name"));
}
}
ListViewAnimal.ItemsSource = this.ListViewItems.View;
这是一个尝试,不是最终代码,但我得到了一个奇怪的结果,一个项目有两个组名。
有什么想法吗 ?