简体   繁体   中英

How do I show both names in GroupDescription from a ListView

First I will explain my problem, then I ask...

I have three entities, Owner, Protector and Animal... The Animal can have one Owner or one Protector, so far, so good...

But, when I will group this in a ListView, if I put PropertyGroupDescription for Owner.Name, if the animal has a Protector as its owner, in ListView will be blank this group.

My Entities:

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 and Protectors derives from BaseOwner.

How can I show, in Group Description, the Owner's name or Protector's name ?

Ps.: First, sorry about my english and, if isn't clear, I will try reformulate.

Ps.2: I'm using MVVM.

UPDATE

I did make some progress here:

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;

This is a try, not the final code, but I'm getting a weird result, with two groups names for one item.

Any thoughts ?

A simple way to solve your problem would be to define a new Property with only a getter in your Animal class and use this property for your grouping.

Below I have shown a sample implementation of the 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";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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