首先我会解释我的问题,然后我会问......
我有三个实体,所有者,保护者和动物...... 动物可以有一个所有者或一个保护者,到目前为止,这么好......
但是,当我在ListView中对此进行分组时,如果我为Owner.Name放置PropertyGroupDescription,如果动物有一个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; }
}
所有者和保护者派生自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;
这是一次尝试,而不是最终的代码,但我得到一个奇怪的结果,一个项目有两个组名。
有什么想法吗?
答案 0 :(得分:2)
解决问题的一种简单方法是在Property
类中定义一个只有getter
的新Animal
,并使用此属性进行分组。
下面我展示了getter
public partial class Animal : Base Model
{
public virtual Owner Owner { get; set; }
public virtual Protector Protector { get; set; }
public string ActiveCareTacker
{
get
{
if (Owner != null && string.IsNullOrEmpty(Owner.Name) == false)
return Owner.Name;
if (Protector != null && string.IsNullOrEmpty(Protector.Name) == false)
return Protector.Name;
return "No Owner/Protector";
}
}
}