AutoGeneratingColumn in DataGrid

Sebastian29

Erfahrenes Mitglied
Hallo!

Es geht um AutoGeneratingColumn="OnAutoGeneratingColumn" im DataGrid.



Bei der Spalte "Adresse" der Tabelle werden nicht alle Spalten der Adresse ( siehe Address.cs ) angezeigt. Warum, ist mir klar, aber mir fällt nun nicht ein, was ich in der Methode "OnAutoGeneratingColumn" implementieren muss, um an die weiteren Annotationen "DisplayName(...)" der Adresse in die Tabelle zu füllen?

Folgende Spalten soll es aussehen:

ID | Title | Firstname | Lastname | ID-Adresse | Strasse | PLZ | Stadt | Land | Telefon | Telefax | Email | ..

Im Google habe ich dies überall gesucht und es gibt dafür keine konkreten Beispiele. Es kam nur für die Standardtypen wie Integer, Double, Enum, usw. raus.


KundenView.xaml.cs

C#:
protected void OnAutoGeneratingColumn( object sender, DataGridAutoGeneratingColumnEventArgs e )
        {
            var displayName = GetPropertyDisplayName( e.PropertyDescriptor );

            var helper = new DisplayNameHelper();
            var type = Type.GetType( e.PropertyType.FullName );

            if ( !string.IsNullOrEmpty( displayName ) )
            {
                e.Column.Header = displayName;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private static string GetPropertyDisplayName( object descriptor )
        {
            var pd = descriptor as PropertyDescriptor;
            if ( pd != null )
            {
                var attr = pd.Attributes[ typeof( DisplayNameAttribute ) ] as DisplayNameAttribute;

                if ( ( attr != null ) && ( attr != DisplayNameAttribute.Default ) )
                {
                    return attr.DisplayName;
                }
            }
            else
            {
                var pi = descriptor as PropertyInfo;
                if ( pi != null )
                {
                    var attrs = pi.GetCustomAttributes( typeof( DisplayNameAttribute ), true );
                    foreach ( var att in attrs )
                    {
                        var attribute = att as DisplayNameAttribute;
                        if ( ( attribute != null ) && ( !Equals( attribute, DisplayNameAttribute.Default ) ) )
                        {
                            return attribute.DisplayName;
                        }
                    }
                }
            }
            return null;
        }

Person.cs

C#:
public class Person
    {
        [DisplayName( @"ID" )]
        public int Id { get; set; }

        [DisplayName( @"Titel" )]
        public string Title { get; set; }

        [DisplayName( @"Vorname" )]
        public string Firstname { get; set; }

        [DisplayName( @"Nachname" )]
        public string Lastname { get; set; }

        [DisplayName( @"Adresse" )]
        public Address Address { get; set; }

        [DisplayName( @"Telefon" )]
        public string Telefon { get; set; }

        [DisplayName( @"Telefax" )]
        public string Telefax { get; set; }

        [DisplayName( @"Email" )]
        public string Email { get; set; }

        [DisplayName( @"Geburtsdatum" )]
        public DateTime? Birthday { get; set; }
    }

Address.cs

C#:
    public class Address
    {
        [DisplayName( @"ID" )]
        public int Id { get; set; }

        [DisplayName( @"Strasse" )]
        public string Street { get; set; }

        [DisplayName( @"PLZ" )]
        public string ZipCode { get; set; }

        [DisplayName( @"Stadt" )]
        public string City { get; set; }

        [DisplayName( @"Land" )]
        public string Country { get; set; }
    }


Ich hoffe, ihr wisst, was ich meine. :)

Gruß
Sebastian29
 
Zurück