Forum Xamarin.Forms

Chnage Color of Weekend Using XamForms.Controls.Calendar

ChiChiChiChi Member ✭✭
edited December 2019 in Xamarin.Forms

Now I am developing app Using Xamairn. In my Project I need to Change color of Satday and Sunday. Is there any way to Only Change Color of SaturdayDay and SunDay in XamForms.Controls.Calendar using Xamarin.
my app calenar design

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Is there any way to Only Change Color of SaturdayDay and SunDay in XamForms.Controls.Calendar using Xamarin.

    Do you mean the selected date's background color ?

    public partial class Page2 : ContentPage
    {
        Calendar c;
        public Page2()
        {
            InitializeComponent();
    
            c = new Calendar
            {
                BorderColor = Color.Gray,
                BorderWidth = 3,
                BackgroundColor = Color.Gray,
                StartDay = DayOfWeek.Sunday,
                StartDate = DateTime.Now,
                SelectedBorderColor = Color.Transparent,               
            };
    
            c.DateClicked += C_DateClicked;
    
            Content = c;
        }
    
        private void C_DateClicked(object sender, DateTimeEventArgs e)
        {
            if (e.DateTime.DayOfWeek == DayOfWeek.Saturday || e.DateTime.DayOfWeek == DayOfWeek.Sunday)   //weekend
            {
                c.SelectedBackgroundColor = Color.Red;
            }
            else
            {
                c.SelectedBackgroundColor = Color.Blue;
            }
        }
    }
    

  • ChiChiChiChi Member ✭✭

    I want to Change Saturday and Sunday Text Color to Gray

  • ChiChiChiChi Member ✭✭

    @ColeX said:

    @TTW said:
    I want to Change Saturday and Sunday Text Color to Gray

    Well , it's a bit of complicated , you have to pick the weekends in a certain month , and set them as Specific Dates !

    Code

    public partial class Page2 : ContentPage
    {
        Calendar c;
        public Page2()
        {
            InitializeComponent();
    
            c = new Calendar
            {
                BorderColor = Color.Gray,
                BorderWidth = 3,
                BackgroundColor = Color.Gray,
                StartDay = DayOfWeek.Monday,
                StartDate = DateTime.Now,
                SelectedBorderColor = Color.Transparent,  
                WeekdaysTextColor = Color.Purple
            };
    
            c.LeftArrowClicked += C_LeftArrowClicked;
            c.RightArrowClicked += C_RightArrowClicked;
    
            Content = c;
    
    
            var now = DateTime.Now;
            var month = now.Month;
            while (now.Month == month)
            {
                now = now.AddDays(-1);
            }
            now = now.AddDays(1);
            pickUpWeekendInMonth(now);
    
        }
    
        private void C_RightArrowClicked(object sender, DateTimeEventArgs e)
        { 
            pickUpWeekendInMonth(e.DateTime);
        }
    
        private void C_LeftArrowClicked(object sender, DateTimeEventArgs e)
        {
            pickUpWeekendInMonth(e.DateTime);
        }
    
    
        private void pickUpWeekendInMonth(DateTime e)
        {
            var list = new List<SpecialDate>();
    
            var firstDay = e;
    
            var month = firstDay.Month;
    
            while (firstDay.Month == month)
            {
                if (firstDay.DayOfWeek == DayOfWeek.Saturday || firstDay.DayOfWeek == DayOfWeek.Sunday)
                {
                    list.Add(new SpecialDate(firstDay) { TextColor = Color.Gray });
                }
    
                firstDay = firstDay.AddDays(1);
            }
    
            c.SpecialDates = list;
        }
    
    }
    

    @ColeX said:

    @TTW said:
    I want to Change Saturday and Sunday Text Color to Gray

    Well , it's a bit of complicated , you have to pick the weekends in a certain month , and set them as Specific Dates !

    Code

    public partial class Page2 : ContentPage
    {
        Calendar c;
        public Page2()
        {
            InitializeComponent();
    
            c = new Calendar
            {
                BorderColor = Color.Gray,
                BorderWidth = 3,
                BackgroundColor = Color.Gray,
                StartDay = DayOfWeek.Monday,
                StartDate = DateTime.Now,
                SelectedBorderColor = Color.Transparent,  
                WeekdaysTextColor = Color.Purple
            };
    
            c.LeftArrowClicked += C_LeftArrowClicked;
            c.RightArrowClicked += C_RightArrowClicked;
    
            Content = c;
    
    
            var now = DateTime.Now;
            var month = now.Month;
            while (now.Month == month)
            {
                now = now.AddDays(-1);
            }
            now = now.AddDays(1);
            pickUpWeekendInMonth(now);
    
        }
    
        private void C_RightArrowClicked(object sender, DateTimeEventArgs e)
        { 
            pickUpWeekendInMonth(e.DateTime);
        }
    
        private void C_LeftArrowClicked(object sender, DateTimeEventArgs e)
        {
            pickUpWeekendInMonth(e.DateTime);
        }
    
    
        private void pickUpWeekendInMonth(DateTime e)
        {
            var list = new List<SpecialDate>();
    
            var firstDay = e;
    
            var month = firstDay.Month;
    
            while (firstDay.Month == month)
            {
                if (firstDay.DayOfWeek == DayOfWeek.Saturday || firstDay.DayOfWeek == DayOfWeek.Sunday)
                {
                    list.Add(new SpecialDate(firstDay) { TextColor = Color.Gray });
                }
    
                firstDay = firstDay.AddDays(1);
            }
    
            c.SpecialDates = list;
        }
    
    }
    

    Thankyou very much.
    I try with your code is Ok.Finally I Can Change the Text Color of WeekEnd day (sat and sun),but I Can't Click the Sat and Sun Date.
    Any way to Click SpecialDate.
    If Can Click ,the Calendar is Perfect.

  • ColeXColeX Member, Xamarin Team Xamurai

    Set Selectable as true on SpecialDate .

                if (firstDay.DayOfWeek == DayOfWeek.Saturday || firstDay.DayOfWeek == DayOfWeek.Sunday)
                {
                    list.Add(new SpecialDate(firstDay) { TextColor = Color.Gray ,Selectable = true});    //change this line
                }
    
Sign In or Register to comment.