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

            me.State = (BusySignalState)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();
        }