using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace DependencyPropertyExample
{
    // Example 18-23. Registering the attached placeholder property

    public class ControlWithPlaceholder : Control
    {
        public static readonly DependencyProperty IsMyPlaceholderProperty;

        static ControlWithPlaceholder()
        {

            PropertyMetadata isMyPlaceholderMetadata = new PropertyMetadata(false,
                new PropertyChangedCallback(OnIsMyPlaceholderChanged));

            IsMyPlaceholderProperty = DependencyProperty.RegisterAttached(
                "IsMyPlaceholder", typeof(bool),
                typeof(ControlWithPlaceholder), isMyPlaceholderMetadata);
        }

        public static bool GetIsMyPlaceholder(DependencyObject target)
        {
            return (bool) target.GetValue(IsMyPlaceholderProperty);
        }
        public static void SetIsMyPlaceholder(DependencyObject target, bool value)
        {
            target.SetValue(IsMyPlaceholderProperty, value);
        }

        // End of Example 18-23


        // Example 18-24. Discovering when the placeholder property is applied

        static void OnIsMyPlaceholderChanged(DependencyObject target,
                                DependencyPropertyChangedEventArgs e)
        {

            FrameworkElement targetElement = target as FrameworkElement;
            if (targetElement != null && GetIsMyPlaceholder(targetElement))
            {
                ControlWithPlaceholder containingControl =
                    targetElement.TemplatedParent as ControlWithPlaceholder;
                if (containingControl != null)
                {
                    containingControl.placeholder = targetElement;
                }
            }
        }

        FrameworkElement placeholder;

        // End of Example 18-24.
    }
}
