using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.Extended;
using System.Windows.Data;

namespace PropertyGrid_CustomEditor
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            //add a custom editor to edit nested Address field
            c1pg.AvailableEditors.Insert(0, new AddressEditor());
            Person p = new Person() 
            {
                ContactInfo  = new Address() { country = "Country", street = "Street", zip = 123 }, FirstName = "Name"
            };

            c1pg.SelectedObject = p;
         }

     
    }

  public  class Person
    {
        public string FirstName { get; set; }
        public Address ContactInfo { get; set; }
    }

   public class Address
    {
        public string country { get; set; }
        public string street { get; set; }
        public int zip { get; set; }
    }



    //custom editor
    public class AddressEditor : C1Expander, ITypeEditorControl
    {

        #region ITypeEditorControl Members
     
        public bool Supports(PropertyAttribute Property)
        {
            return Property.PropertyInfo.PropertyType == typeof(Address);
        }

        public ITypeEditorControl Create()
        {
            
            return new AddressEditor();
        }

        public void Attach(PropertyAttribute property)
        {
            var editor = new C1PropertyGrid();
            editor.SelectedObject = (property.SelectedObject as Person).ContactInfo;
            this.Content = editor;  
        }

        public void Detach(PropertyAttribute property)
        {
        }

        public event System.ComponentModel.PropertyChangedEventHandler ValueChanged;

        #endregion
    }

}
