private bool AddDateToCalendarSelection(CalendarButtonContent content)
 {
     if (this.calendar == null)
     {
         return(false);
     }
     return(this.calendar.Select(content.Date, true));
 }
        private bool AddDate(object sender)
        {
            CalendarButton        btn     = sender as CalendarButton;
            CalendarButtonContent content = null;

            if (btn != null)
            {
                content = btn.Content as CalendarButtonContent;
            }
            if (content != null)
            {
                return(this.AddDateToCalendarSelection(content));
            }

            return(false);
        }
        /// <summary>
        /// Called when the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property changes.
        /// </summary>
        /// <param name="oldContent">The old value of the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property.</param>
        /// <param name="newContent">The new value of the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property.</param>
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            CalendarButtonContent buttonContent = newContent as CalendarButtonContent;

            if (buttonContent != null)
            {
                buttonContent.Owner    = this;
                this.ButtonType        = buttonContent.ButtonType;
                this.IsFromCurrentView = buttonContent.IsFromCurrentView;
                this.IsInAnotherView   = buttonContent.IsInAnotherView;
                this.IsSelected        = buttonContent.IsSelected;
                this.Text      = buttonContent.Text;
                this.IsEnabled = buttonContent.IsEnabled;
            }
        }
        /// <summary>
        /// When overridden in a derived class, undoes the effects of the <see cref="M:System.Windows.Controls.ItemsControl.PrepareContainerForItemOverride(System.Windows.DependencyObject,System.Object)"/> method.
        /// </summary>
        /// <param name="element">The container element.</param>
        /// <param name="item">The item.</param>
        protected override void ClearContainerForItemOverride(DependencyObject element, object item)
        {
            CalendarButton button = element as CalendarButton;

            if (button != null)
            {
                button.MouseEnter -= new System.Windows.Input.MouseEventHandler(this.CalendarButtonMouseEnter);
                ////button.MouseLeftButtonDown -= new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonDown);
                ////button.MouseLeftButtonUp -= new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonUp);
                button.RemoveHandler(CalendarButton.MouseLeftButtonDownEvent, new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonDown));
                button.RemoveHandler(CalendarButton.MouseLeftButtonUpEvent, new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonUp));
            }
            CalendarButtonContent content = item as CalendarButtonContent;

            if (content != null && this.DateToContent.ContainsKey(content.Date))
            {
                this.DateToContent.Remove(content.Date);
            }

            base.ClearContainerForItemOverride(element, item);
        }
        /// <summary>
        /// Prepares the specified element to display the specified item.
        /// </summary>
        /// <param name="element">Element used to display the specified item.</param>
        /// <param name="item">Specified item.</param>
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);

            CalendarButtonContent content = item as CalendarButtonContent;


            StyleManager.SetTheme(element, StyleManager.GetTheme(this));


            if (content != null)
            {
                // This check fixed an integratin problem with DataForm
                // TODO: Investigate why this check fixed an integratin problem with DataForm
                if (!this.DateToContent.ContainsKey(content.Date))
                {
                    this.DateToContent.Add(content.Date, content);
                }
            }

            CalendarButton button = element as CalendarButton;

            if (button != null && button != item)
            {
                if (content.ButtonType == CalendarButtonType.WeekName ||
                    content.ButtonType == CalendarButtonType.WeekNumber)
                {
                    button.IsTabStop = false;

                    string propertyPath = content.ButtonType == CalendarButtonType.WeekName ? "HideRow" : "HideColumn";

                    Binding binding = new Binding(propertyPath)
                    {
                        Source    = this,
                        Converter = new BoleanToVisibilityConverterRevert(),
                        Mode      = BindingMode.TwoWay
                    };
                    button.SetBinding(Button.VisibilityProperty, binding);

                    if (this.itemsPanel == null)
                    {
                        var calendarPanel = this.GetItemsPanel <CalendarPanel>();
                        if (calendarPanel != null)
                        {
                            this.itemsPanel = calendarPanel;

                            calendarPanel.SetBinding(CalendarPanel.HideFirstColumnProperty, new Binding("HideColumn")
                            {
                                Source = this
                            });
                            calendarPanel.SetBinding(CalendarPanel.HideFirstRowProperty, new Binding("HideRow")
                            {
                                Source = this
                            });
                        }
                    }
                }
                button.Content     = item;
                button.MouseEnter += new System.Windows.Input.MouseEventHandler(this.CalendarButtonMouseEnter);
                ////button.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonDown);
                ////button.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonUp);
                button.AddHandler(CalendarButton.MouseLeftButtonDownEvent, new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonDown), true);
                button.AddHandler(CalendarButton.MouseLeftButtonUpEvent, new System.Windows.Input.MouseButtonEventHandler(this.CalendarButtonMouseLeftButtonUp), true);

                if (dateToFocus.HasValue && content.Date.Equals(dateToFocus.Value))
                {
                    button.FocusOnLoad = true;
                    dateToFocus        = null;
                }
            }
        }