繁体   English   中英

如何在ListView中显示GroupDescription中的两个名称

[英]How do I show both names in GroupDescription from a ListView

首先,我将解释我的问题,然后我问...

我有三个实体,所有者,保护者和动物......动物可以有一个所有者或一个保护者,到目前为止,这么好......

但是,当我在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; }
}

所有者和保护者派生自BaseOwner。

如何在组描述中显示所有者的姓名或保护者的姓名?

Ps。:首先,抱歉我的英语,如果不清楚,我会尝试重新制定。

Ps.2:我正在使用MVVM。

UPDATE

我在这里取得了一些进展:

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;

这是一个尝试,而不是最终的代码,但我得到一个奇怪的结果,一个项目的两个组名称。

有什么想法吗 ?

解决问题的一种简单方法是在Animal类中定义一个只有getter的新Property ,并使用此属性进行分组。

下面我展示了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";
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM