using System;
using System.Windows.Controls;
using System.Windows.Media;
using C1.Silverlight;
using System.Collections.Generic;
using System.Linq;
namespace C1Extended_Demo
{
	public partial class DemoPropertyGrid : UserControl
	{
		public DemoPropertyGrid()
		{
			InitializeComponent();
			_propertyGrid.SelectedObject = _targetRectangle;
            this.btnTest.Click += new System.Windows.RoutedEventHandler(btnTest_Click);
		}

        void btnTest_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.btnTest.Content = "content changed";
        }

		private void _propertyGrid_PropertyBoxesLoaded(object sender, EventArgs e)
		{
			foreach (var pBox in _propertyGrid.PropertyBoxes)
			{
				pBox.ResetValue();
			} 
		}
        private void CheckBox_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (e.OriginalSource != sender) return;
            this.UpdateCheckContent();
        }

        private void UpdateCheckContent()
        {
            CheckBox[] elements = this.grid.Children.Where(x => (x is CheckBox) && (x as CheckBox).IsChecked.Value).Cast<CheckBox>().ToArray();
            List<object> listelement = new List<object>();
            for (int i = 0; i < elements.Length; i++)
            {
                if (elements[i].IsChecked.Value)
                {
                    listelement.Add(elements[i].Content);
                }
            }
            this._propertyGrid.SelectedObject = null;
            if (listelement.Count > 0)
            {
                for (int i = 0; i < listelement.Count; i++)
                {
                    this._propertyGrid.SelectedObjects.Add(listelement[i]);
                }
            }
        }
		public bool ShowResetButton
		{
			get { return _propertyGrid.ShowResetButton; }
			set { _propertyGrid.ShowResetButton = value; }
		}

		public bool ShowDescription 
		{
			get { return _propertyGrid.ShowDescription; }
			set { _propertyGrid.ShowDescription = value; }
		}

		public bool AutoGenerateProperties
		{
			get { return _propertyGrid.AutoGenerateProperties; }
			set { _propertyGrid.AutoGenerateProperties = value; }
		}

		public bool AutoGenerateMethods
		{
			get { return _propertyGrid.AutoGenerateMethods; }
			set { _propertyGrid.AutoGenerateMethods = value; }
		}

		public Brush DEMO_Background
		{
			get
			{
				return _propertyGrid.Background;
			}
			set
			{
				_propertyGrid.Background = value;
			}
		}

		public Brush DEMO_Foreground
		{
			get
			{
				return _propertyGrid.Foreground;
			}
			set
			{
				_propertyGrid.Foreground = value;
			}
		}
		public Brush DEMO_BorderBrush
		{
			get
			{
				return _propertyGrid.BorderBrush;
			}
			set
			{
				_propertyGrid.BorderBrush = value;
			}
		}

		public Brush MouseOverBrush
		{
			get
			{
				return _propertyGrid.MouseOverBrush;
			}
			set
			{
				_propertyGrid.MouseOverBrush = value;
			}
		}

		public Brush PressedBrush
		{
			get
			{
				return _propertyGrid.PressedBrush;
			}
			set
			{
				_propertyGrid.PressedBrush = value;
			}
		}
		public Brush CategoryBackground
		{
			get
			{
				return _propertyGrid.CategoryBackground;
			}
			set
			{
				_propertyGrid.CategoryBackground = value;
			}
		}
		public Brush CategoryForeground
		{
			get
			{
				return _propertyGrid.CategoryForeground;
			}
			set
			{
				_propertyGrid.CategoryForeground = value;
			}
		}

		private void _propertyGrid_PropertyBoxAdded(object sender, C1.Silverlight.Extended.PropertyBoxChangedEventArgs e)
		{
			if (e.PropertyBox.PropertyAttribute.MemberName == "Opacity")
			{
				(e.PropertyBox.CurrentEditor as C1NumericBox).Increment = 0.01;
			}
		}
	}
}
