This is a migrated thread and some comments may be shown as answers.

Change selected date back colour Solution...

3 Answers 159 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Rahul Singh
Top achievements
Rank 1
Rahul Singh asked on 18 Feb 2011, 01:19 PM
HI! All,

    

I have a work around solution to change the background  colour of the selected date. Please if anyone can improve the code or have a better solution can please update me.

 

XAML Code

<Window x:Class="TelerikDateSelectCal.Window1"
    xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"        
    xmlns:local="clr-namespace:TelerikDateSelectCal"
    Title="Window1" Height="300" Width="552"
    xml:lang="en-GB">
    <Window.Resources>
          
        <local:CustomTemplateSelector x:Key="WorksDiaryDayTemplateSelector">
              
            <local:CustomTemplateSelector.SelectedRed>
                <DataTemplate>
                    <Border Background="Red" Width="20">
                        <TextBox Background="Transparent" Text="{Binding Text}"
                        BorderThickness="0"/>
                    </Border>
                </DataTemplate>
            </local:CustomTemplateSelector.SelectedRed>
  
            <local:CustomTemplateSelector.SelectedGreen>
                <DataTemplate>
                    <Border Background="Green" Width="20">
                        <TextBox Background="Transparent" Text="{Binding Text}"
                        BorderThickness="0"/>
                    </Border>
                </DataTemplate>
            </local:CustomTemplateSelector.SelectedGreen>
  
            <local:CustomTemplateSelector.SelectedBlue>
                <DataTemplate>
                    <Border Background="Blue" Width="20">
                        <TextBox Background="Transparent" Text="{Binding Text}"
                        BorderThickness="0"/>
                    </Border>
                </DataTemplate>
            </local:CustomTemplateSelector.SelectedBlue>
  
            <local:CustomTemplateSelector.SelectedYellow>
                <DataTemplate>
                    <Border Background="Yellow" Width="20">
                        <TextBox Background="Transparent" Text="{Binding Text}"
                        BorderThickness="0"/>
                    </Border>
                </DataTemplate>
            </local:CustomTemplateSelector.SelectedYellow>
  
            <local:CustomTemplateSelector.SelectedBlank>
                <DataTemplate x:Name="Test">
                        <TextBox Background="Transparent" Text="{Binding Text}"
                        BorderThickness="0" Width="20"/>
                </DataTemplate>
                </local:CustomTemplateSelector.SelectedBlank>
        </local:CustomTemplateSelector>
  
  
  
    </Window.Resources>
      
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="20*"/>
        </Grid.ColumnDefinitions>
          
        <Grid Grid.Column="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="20*"/>
            </Grid.ColumnDefinitions>
  
            <StackPanel Grid.Column="0">
                <Button Name="btnRed" Width="20" Height="20" Margin="3" Background="Red" Click="SetComboOpen"/>
                <Button Name="btnGreen" Width="20" Height="20" Margin="3" Background="Green" Click="SetComboOpen"/>
                <Button Name="btnBlue" Width="20" Height="20" Margin="3" Background="Blue" Click="SetComboOpen"/>
                <Button Name="btnYellow" Width="20" Height="20" Margin="3" Background="Yellow" Click="SetComboOpen"/>
                  
                <TextBox Margin="4" Name="txt1" Height="50" BorderBrush="BlueViolet" TextWrapping="Wrap"
                         Width="150"></TextBox>
                  
            </StackPanel>
              
            <StackPanel Grid.Column="1">
                <ComboBox Name="cmbRed" Width="175" Margin="2" DropDownClosed="SetCacBackColor" >
                    <ComboBoxItem>Red</ComboBoxItem>
                </ComboBox>
                <ComboBox Name="cmbGreen" Width="175" Margin="2" DropDownClosed="SetCacBackColor">
                    <ComboBoxItem>Green</ComboBoxItem>
                </ComboBox>
                <ComboBox Name="cmbBlue" Width="175" Margin="2" DropDownClosed="SetCacBackColor">
                    <ComboBoxItem>Blue</ComboBoxItem>
                </ComboBox>
                <ComboBox Name="cmbYellow" Width="175" Margin="2" DropDownClosed="SetCacBackColor">
                    <ComboBoxItem>Yellow</ComboBoxItem>
                </ComboBox>
            </StackPanel>
        </Grid>
  
        <telerik:RadCalendar x:Name="calendar" Grid.Column="1"
                Margin="2" SelectionMode="Single"
                IsTodayHighlighted="True" ViewsHeaderVisibility="Collapsed" Culture="en-GB"
                Rows="3" Columns="4" 
                SelectionChanged="calendar_SelectionChanged"
                DayTemplateSelector="{StaticResource WorksDiaryDayTemplateSelector}"/>
    </Grid>
</Window>

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
  
using Telerik;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Calendar;
  
using System.ComponentModel;
using System.Collections.ObjectModel;
  
using System.Data;
#if !SILVERLIGHT
using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
using DataTemplateSelector = System.Windows.Controls.DataTemplateSelector;
#endif
  
namespace TelerikDateSelectCal
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        List<DateTime> lstRedDate;
        List<DateTime> lstGreenDate;
        List<DateTime> lstBlueDate;
        List<DateTime> lstYellowDate;
  
        public Window1()
        {
            InitializeComponent();
  
            lstRedDate = new List<DateTime>();
            lstGreenDate = new List<DateTime>();
            lstBlueDate = new List<DateTime>();
            lstYellowDate = new List<DateTime>();
  
            setAllComboENFalse();
            this.cmbRed.IsEnabled = true;
  
            setSelectedDateGlbColor("Red");
        }
  
        private void SetCacBackColor(object sender, EventArgs e)
        {
  
        }
  
        private void SetComboOpen(object sender, RoutedEventArgs e)
        {
            setAllComboENFalse();
            string selecteColor = "";
  
            if (sender == btnRed)
            {
                this.cmbRed.IsEnabled = true;
                selecteColor = "Red";
            }
            else if (sender == btnGreen)
            {
                this.cmbGreen.IsEnabled = true;
                selecteColor = "Green";
            }
            else if (sender == btnBlue)
            {
                this.cmbBlue.IsEnabled = true;
                selecteColor = "Blue";
            }
            else if (sender == btnYellow)
            {
                this.cmbYellow.IsEnabled = true;
                selecteColor = "Yellow";
            }
  
            setSelectedDateGlbColor(selecteColor);
        }
  
        protected void setSelectedDateGlbColor(string selectedColorName)
        {
            if (Application.Current.Properties.Contains("SelectedColor"))
                Application.Current.Properties.Remove("SelectedColor");
  
            Application.Current.Properties.Add("SelectedColor", selectedColorName);
        }
  
        protected void setAllComboENFalse()
        {
            this.cmbRed.IsEnabled = false;
            this.cmbGreen.IsEnabled = false;
            this.cmbBlue.IsEnabled = false;
            this.cmbYellow.IsEnabled = false;
        }
  
        private void calendar_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Application.Current.Properties.Contains("SelectedColor"))
            {
                string selectedColor = Application.Current.Properties["SelectedColor"].ToString();
  
                switch (selectedColor)
                {
                    case "Red":
                        lstRedDate.Add((DateTime)calendar.SelectedDate.Value);
  
                        if (Application.Current.Properties.Contains("RedColor"))
                            Application.Current.Properties.Remove("RedColor");
  
                        Application.Current.Properties.Add("RedColor", lstRedDate);
  
                        break;
  
                    case "Green":
                        lstGreenDate.Add((DateTime)calendar.SelectedDate.Value);
  
                        if (Application.Current.Properties.Contains("GreenColor"))
                            Application.Current.Properties.Remove("GreenColor");
  
                        Application.Current.Properties.Add("GreenColor", lstGreenDate);
  
                        break;
  
                    case "Blue":
                        lstBlueDate.Add((DateTime)calendar.SelectedDate.Value);
  
                        if (Application.Current.Properties.Contains("BlueColor"))
                            Application.Current.Properties.Remove("BlueColor");
  
                        Application.Current.Properties.Add("BlueColor", lstBlueDate);
  
                        break;
  
                    case "Yellow":
                        lstYellowDate.Add((DateTime)calendar.SelectedDate.Value);
  
                        if (Application.Current.Properties.Contains("YellowColor"))
                            Application.Current.Properties.Remove("YellowColor");
  
                        Application.Current.Properties.Add("YellowColor", lstYellowDate);
  
                        break;
  
                    default:
                        MessageBox.Show("Invalid Date Color....", "Message....", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        break;
                }
  
  
                DateTime tempDate;
                tempDate = new DateTime(calendar.DisplayDate.Year, calendar.DisplayDate.Month, 1);
                calendar.DisplayDate = tempDate.AddYears(8);
                calendar.DisplayDate = tempDate;
            }
            else
            {
                MessageBox.Show("Please Select a date Color ", "Message....", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }
    }
  
    [Serializable()]
    public class CustomTemplateSelector : DataTemplateSelector
    {
        // ObservableCollection<Object> listTabsValue = new ObservableCollection<object>();
  
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            CalendarButtonContent content = item as CalendarButtonContent;
            string daySelColorFlg = "";
            List<DateTime> lstDtRed = (List<DateTime>)Application.Current.Properties["RedColor"];
            List<DateTime> lstDtGreen = (List<DateTime>)Application.Current.Properties["GreenColor"];
            List<DateTime> lstDtBlue = (List<DateTime>)Application.Current.Properties["BlueColor"];
            List<DateTime> lstDtYellow = (List<DateTime>)Application.Current.Properties["YellowColor"];
  
            if (Application.Current.Properties.Contains("SelectedColor"))
            {
                daySelColorFlg = Application.Current.Properties["SelectedColor"].ToString();
            }
            else
                return SelectedBlank ;
              
  
            if (content.ButtonType == CalendarButtonType.Date
                || content.ButtonType == CalendarButtonType.TodayDate)
            {
                switch (daySelColorFlg)
                {
                    case "Red":
                          
                        if (lstDtRed != null)
                        {
                            if (lstDtRed.Contains(content.Date))
                                return SelectedRed;
                            else if (lstDtGreen != null || lstDtBlue != null || lstDtYellow != null)
                            {
                                if(lstDtGreen != null)
                                    if (lstDtGreen.Contains(content.Date))
                                        return SelectedGreen;
  
                                if (lstDtBlue != null)
                                    if (lstDtBlue.Contains(content.Date))
                                        return SelectedBlue;
  
                                if (lstDtYellow != null)
                                    if (lstDtYellow.Contains(content.Date))
                                    return SelectedYellow;
                            }
                            else
                                return SelectedBlank;
                        }
                        else
                            return SelectedBlank;
  
                        break;
  
                    case "Green":
                          
                        if (lstDtGreen != null)
                        {
                            if (lstDtGreen.Contains(content.Date))
                                return SelectedGreen;
  
                            else if (lstDtRed != null || lstDtBlue != null || lstDtYellow != null)
                            {
                                if (lstDtRed != null)
                                    if (lstDtRed.Contains(content.Date))
                                        return SelectedRed;
  
                                if (lstDtBlue != null)
                                    if (lstDtBlue.Contains(content.Date))
                                        return SelectedBlue;
  
                                if (lstDtYellow != null)
                                    if (lstDtYellow.Contains(content.Date))
                                        return SelectedYellow;
                            }
                            else
                                return SelectedBlank;
                        }
                        else
                            return SelectedBlank;
  
                        break;
  
                    case "Blue":
  
                        if (lstDtBlue != null)
                        {
                            if (lstDtBlue.Contains(content.Date))
                                return SelectedBlue;
                            else if (lstDtRed != null || lstDtGreen != null || lstDtYellow != null)
                            {
                                if (lstDtRed != null)
                                    if (lstDtRed.Contains(content.Date))
                                        return SelectedRed;
                                if (lstDtGreen != null)
                                    if (lstDtGreen.Contains(content.Date))
                                        return SelectedGreen;
                                if (lstDtYellow != null)
                                    if (lstDtYellow.Contains(content.Date))
                                        return SelectedYellow;
                            }
                            else
                                return SelectedBlank;
                        }
                        else
                            return SelectedBlank;
  
                        break;
  
                    case "Yellow":
  
                        if (lstDtYellow != null)
                        {
                            if (lstDtYellow.Contains(content.Date))
                                return SelectedYellow;
                            else if (lstDtRed != null || lstDtGreen != null || lstDtBlue != null)
                            {
                                if (lstDtRed != null)
                                    if (lstDtRed.Contains(content.Date))
                                        return SelectedRed;
  
                                if (lstDtGreen != null)
                                    if (lstDtGreen.Contains(content.Date))
                                        return SelectedGreen;
  
                                if (lstDtBlue != null)
                                    if (lstDtBlue.Contains(content.Date))
                                        return SelectedBlue;
                            }
                            else
                                return SelectedBlank;
                        }
                        else
                            return SelectedBlank;
  
                        break;
  
                    default:
  
                        break;
                }
  
            }
            return SelectedBlank;
        }
  
        public DataTemplate SelectedRed
        {
            get;
            set;
        }
  
        public DataTemplate SelectedBlue
        {
            get;
            set;
        }
  
        public DataTemplate SelectedGreen
        {
            get;
            set;
        }
  
        public DataTemplate SelectedYellow
        {
            get;
            set;
        }
  
        public DataTemplate SelectedBlank
        {
            get;
            set;
        }
    }
}


Not best of the code, it's too lengthy and needs improvement so please le me know if you have improved it.

 

Thanks

Rahul


3 Answers, 1 is accepted

Sort by
0
Rahul Singh
Top achievements
Rank 1
answered on 18 Feb 2011, 01:28 PM
0
Kaloyan
Telerik team
answered on 22 Feb 2011, 02:37 PM
Hello Rahul Singh,

You can actually refresh the calendar when using the following code:

var template = calendar.DayTemplateSelector;
calendar.DayTemplateSelector = null;
calendar.DayTemplateSelector = template;

Regards,
Kaloyan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Rahul Singh
Top achievements
Rank 1
answered on 22 Feb 2011, 04:37 PM
HI! Kaloyan,

    Thanks a lot for the update, your comments has improved the speed of the application.

Thanks
Rahul Singh
Tags
Calendar
Asked by
Rahul Singh
Top achievements
Rank 1
Answers by
Rahul Singh
Top achievements
Rank 1
Kaloyan
Telerik team
Share this question
or