protected static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CloseButton me = d as CloseButton;

            me.State = (CloseButtonState)e.NewValue;
            me.ApplyState();
        }
        // Locate Elements and Add Event Listeners
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            TitlePanel = this.GetTemplateChild(_TITLE_PANEL) as StackPanel;
            if (TitlePanel == null)
            {
                throw new Exception("Apply Template Error: Failed to get the Title Panel");
            }

            HeaderPanel = this.GetTemplateChild(_HEADER_PANEL) as StackPanel;
            if (HeaderPanel == null)
            {
                throw new Exception("Apply Template Error: Failed to get the Header Panel");
            }

            BusyAnimation = this.GetTemplateChild(_BUSY_ANIMATION) as BusySignal;
            if (BusyAnimation != null)
            {
                BusyAnimation.State = IsBusy ? BusySignalState.STATE_BUSY : BusySignalState.STATE_HIDE;
            }

            WatermarkText = this.GetTemplateChild(_WATERMARK_TEXT) as TextBlock;
            if (WatermarkText != null)
            {
                WatermarkText.Visibility = Visibility.Collapsed;
            }

            ContentContainer = this.GetTemplateChild(_CONTENT_CONTAINER) as FrameworkElement;
            if (ContentContainer != null)
            {
                ContentContainer.SizeChanged += new SizeChangedEventHandler(ContentContainer_SizeChanged);
                if (!IsExpanded)
                {
                    ContentContainer.Visibility = Visibility.Collapsed;
                }
            }

            ContentPanel = this.GetTemplateChild(_CONTENT_PANEL) as FrameworkElement;
            if (ContentPanel != null)
            {
                ContentPanel.SizeChanged += new SizeChangedEventHandler(ContentPanel_SizeChanged);
            }

            IconImage = this.GetTemplateChild(_WINDOW_ICON) as Image;
            if (IconImage != null)
            {
                if (IconSource != null)
                {
                    IconImage.Source = IconSource;
                }
                else
                {
                    IconImage.Visibility = Visibility.Collapsed;
                }
            }

            TitleBlock = this.GetTemplateChild(_TITLE_BLOCK) as TextBlock;
            if (TitleBlock != null)
            {
                if (TitleFontSize > 0.0)
                {
                    TitleBlock.FontSize = TitleFontSize;
                }
                if (TitleForeground != null)
                {
                    TitleBlock.Foreground = TitleForeground;
                }
            }

            WindowToggleButton = this.GetTemplateChild(_BUTTON_TOGGLE) as ToggleButton;
            WindowCloseButton  = this.GetTemplateChild(_BUTTON_CLOSE) as CloseButton;

            if (WindowToggleButton != null)
            {
                WindowToggleButton.Click += new RoutedEventHandler(WindowToggleButton_Click);
            }

            if (WindowCloseButton != null)
            {
                WindowCloseButton.Click += new RoutedEventHandler(WindowCloseButton_Click);
            }

            if (IsResizable)
            {
                FrameworkElement resizebarN = GetTemplateChild(_RESIZEBAR_NORTH) as FrameworkElement;
                FrameworkElement resizebarS = GetTemplateChild(_RESIZEBAR_SOUTH) as FrameworkElement;
                FrameworkElement resizebarW = GetTemplateChild(_RESIZEBAR_WEST) as FrameworkElement;
                FrameworkElement resizebarE = GetTemplateChild(_RESIZEBAR_EAST) as FrameworkElement;

                if (resizebarN != null && resizebarS != null && resizebarW != null && resizebarE != null)
                {
                    resizebarN.Cursor = Cursors.SizeNS;
                    resizebarS.Cursor = Cursors.SizeNS;
                    resizebarW.Cursor = Cursors.SizeWE;
                    resizebarE.Cursor = Cursors.SizeWE;

                    resizebarN.MouseLeftButtonDown += new MouseButtonEventHandler(Resizebar_MouseLeftButtonDown);
                    resizebarS.MouseLeftButtonDown += new MouseButtonEventHandler(Resizebar_MouseLeftButtonDown);
                    resizebarW.MouseLeftButtonDown += new MouseButtonEventHandler(Resizebar_MouseLeftButtonDown);
                    resizebarE.MouseLeftButtonDown += new MouseButtonEventHandler(Resizebar_MouseLeftButtonDown);

                    resizebarN.MouseMove += new MouseEventHandler(Resizebar_MouseMove);
                    resizebarS.MouseMove += new MouseEventHandler(Resizebar_MouseMove);
                    resizebarW.MouseMove += new MouseEventHandler(Resizebar_MouseMove);
                    resizebarE.MouseMove += new MouseEventHandler(Resizebar_MouseMove);

                    resizebarN.MouseLeftButtonUp += new MouseButtonEventHandler(Resizebar_MouseLeftButtonUp);
                    resizebarS.MouseLeftButtonUp += new MouseButtonEventHandler(Resizebar_MouseLeftButtonUp);
                    resizebarW.MouseLeftButtonUp += new MouseButtonEventHandler(Resizebar_MouseLeftButtonUp);
                    resizebarE.MouseLeftButtonUp += new MouseButtonEventHandler(Resizebar_MouseLeftButtonUp);
                }
            }

            if (this.IsExpanded && this.DisableAnimation)
            {
                this.Focus();
            }
            this.isTemplateApplied = true;
            this.OnOpen();
        }
        // Locate Elements and Add Event Listeners
        public override void OnApplyTemplate()
        {
            WindowTitleBlock = this.GetTemplateChild(_TITLE_BLOCK) as TextBlock;
            if (WindowTitleBlock != null)
            {
                if (TitleFontSize > 0.0)
                {
                    WindowTitleBlock.FontSize = TitleFontSize;
                }
                if (TitleForeground != null)
                {
                    WindowTitleBlock.Foreground = TitleForeground;
                }
                WindowTitleBlock.Text         = string.Format(TitleFormat, Title);
                WindowTitleBlock.TextWrapping = TitleWrapping;
            }

            CloseButton WindowCloseButton = this.GetTemplateChild(_BUTTON_CLOSE) as CloseButton;

            if (WindowCloseButton != null)
            {
                if (this.ShowCloseButton)
                {
                    WindowCloseButton.Click += new RoutedEventHandler(WindowCloseButton_Click);
                }
                else
                {
                    WindowCloseButton.Visibility = System.Windows.Visibility.Collapsed;
                }
            }

            if (ShowArrow)
            {
                Shape ArrowUpperLeft  = this.GetTemplateChild(_ARROW_UPPER_LEFT) as Shape;
                Shape ArrowLowerLeft  = this.GetTemplateChild(_ARROW_LOWER_LEFT) as Shape;
                Shape ArrowUpperRight = this.GetTemplateChild(_ARROW_UPPER_RIGHT) as Shape;
                Shape ArrowLowerRight = this.GetTemplateChild(_ARROW_LOWER_RIGHT) as Shape;
                switch (this.ArrowDirection)
                {
                case Controls.ArrowDirection.UpperLeft:
                    ArrowUpperLeft.Visibility = System.Windows.Visibility.Visible; break;

                case Controls.ArrowDirection.LowerLeft:
                    ArrowLowerLeft.Visibility = System.Windows.Visibility.Visible; break;

                case Controls.ArrowDirection.UpperRight:
                    ArrowUpperRight.Visibility = System.Windows.Visibility.Visible; break;

                case Controls.ArrowDirection.LowerRight:
                    ArrowLowerRight.Visibility = System.Windows.Visibility.Visible; break;
                }
            }

            if (IsFloatable)
            {
                FrameworkElement ControlGrip = this.GetTemplateChild(_CONTROL_GRIP) as FrameworkElement;
                if (ControlGrip != null)
                {
                    ControlGrip.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDownControl);
                    ControlGrip.MouseLeftButtonUp   += new MouseButtonEventHandler(OnMouseLeftButtonUpControl);
                    ControlGrip.MouseMove           += new MouseEventHandler(OnMouseMoveControl);
                }
            }

            if (IsResizable)
            {
                FrameworkElement resizebarS = GetTemplateChild(_RESIZEBAR_SOUTH) as FrameworkElement;
                FrameworkElement resizebarE = GetTemplateChild(_RESIZEBAR_EAST) as FrameworkElement;

                if (resizebarS != null && resizebarE != null)
                {
                    resizebarS.Cursor = Cursors.SizeNS;
                    resizebarE.Cursor = Cursors.SizeWE;

                    resizebarS.MouseLeftButtonDown += new MouseButtonEventHandler(Resizebar_MouseLeftButtonDown);
                    resizebarE.MouseLeftButtonDown += new MouseButtonEventHandler(Resizebar_MouseLeftButtonDown);

                    resizebarS.MouseMove += new MouseEventHandler(Resizebar_MouseMove);
                    resizebarE.MouseMove += new MouseEventHandler(Resizebar_MouseMove);

                    resizebarS.MouseLeftButtonUp += new MouseButtonEventHandler(Resizebar_MouseLeftButtonUp);
                    resizebarE.MouseLeftButtonUp += new MouseButtonEventHandler(Resizebar_MouseLeftButtonUp);
                }
            }
        }