Contribute to the Xamarin Forms Toolkit

13»

Posts

  • DanielNadbabiDanielNadbabi DEMember ✭✭

    Hi Guys,

    I have a problem with CalendatView control in xamarin forms, it's not working as expected. For example, MinDate is not working for any value, it's working by default, today's month, also when I go on the calendar, always is the today's day selected. Did anyone encounter this kind problems before?

    Best regards,
    Daniel

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    You should post your code here, so that somebody can help you...

  • DanielNadbabiDanielNadbabi DEMember ✭✭

    It's something like this.. I have multi-page with tabs (ExtendedTabbedPage class) where this control is rendered twice. One for departure and one for return dates. (If the return date is null, its Departure date, otherwise it's Return date. Both are bonded with corresponding MVVM classes.

        public class CmbCalender : CalendarView
            {
                public static readonly BindableProperty ReturnDateProperty =
                    BindableProperty.Create(
                        "ReturnDate",
                        typeof(DateTime),
                        typeof(CmbCalender),
                        FirstDayOfMonth(DateTime.Today),
                        BindingMode.TwoWay,
                        propertyChanged: OnReturnDatePropertyChanged,
                        coerceValue: CoreceReturnDate);
    
                public static readonly BindableProperty DepartureDateProperty =
                    BindableProperty.Create(
                        "DepartureDate",
                        typeof(DateTime),
                        typeof(CmbCalender),
                        FirstDayOfMonth(DateTime.Today),
                        BindingMode.TwoWay,
                        propertyChanged: OnDepartureDatePropertyChanged);
    
    
        public CmbCalender()
        {
            var date = FirstDayOfMonth(DateTime.Now);
            MinDate = date;
            MaxDate = LastDayOfMonth(DateTime.Now.AddMonths(3));
            HighlightedDateBackgroundColor = Color.FromRgb(227, 227, 227);
            ShouldHighlightDaysOfWeekLabels = true;
            SelectionBackgroundStyle = BackgroundStyle.CircleOutline;
            TodayBackgroundStyle = BackgroundStyle.CircleOutline;
            HighlightedDaysOfWeek = new[] {DayOfWeek.Saturday, DayOfWeek.Sunday};
            ShowNavigationArrows = true;
            MonthTitleFont = Font.OfSize("Open 24 Display St", NamedSize.Medium);
            VerticalOptions = LayoutOptions.Start;
            HorizontalOptions = LayoutOptions.CenterAndExpand;
            DateSelected += OnDateSelectionEvent;
        }
    
        /// <summary>
        ///     Gets or sets the maximum date.
        /// </summary>
        /// <value>The maximum date.</value>
        public DateTime DepartureDate
        {
            get => (DateTime) GetValue(DepartureDateProperty);
            set => SetValue(DepartureDateProperty, value);
        }
    
        private static void OnDepartureDatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var defaultDate = DateTime.Now;
            var cmbCalender = bindable as CmbCalender;
            var value = newValue;
            if (cmbCalender == null || value == null)
                return;
            var actualDate = (DateTime) value;
            if (actualDate == DateTime.MinValue || actualDate == DateTime.MaxValue)
                return;
            cmbCalender.SelectedDate = actualDate;
            var returnDate = cmbCalender.ReturnDate;
            if (returnDate == null || returnDate == DateTime.MaxValue || returnDate == DateTime.MinValue)
                return;
        }
    
        private void OnDateSelectionEvent(object sender, DateTime e)
        {
        }
    
        #region Return date 
    
        private static void OnReturnDatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var defaultDate = DateTime.Now;
            var cmbCalender = bindable as CmbCalender;
            var value = newValue;
            if (cmbCalender == null || value == null)
                return;
            var actualDate = (DateTime) value;
            if (actualDate == DateTime.MinValue || actualDate == DateTime.MaxValue)
                return;
    
            var departureDate = cmbCalender.DepartureDate;
            if (actualDate.Date < departureDate.Date)
                return;
        }
    
        private static object CoreceReturnDate(BindableObject bindable, object value)
        {
            var defaultDate = DateTime.Now;
            var cmbCalender = bindable as CmbCalender;
            if (cmbCalender == null || value == null)
                return defaultDate;
            var actualDate = (DateTime) value;
            if (actualDate == DateTime.MinValue || actualDate == DateTime.MaxValue)
                return defaultDate;
    
            var departureDate = cmbCalender.DepartureDate;
            if (actualDate.Date < departureDate.Date)
                return defaultDate;
            cmbCalender.SelectedDate = actualDate;
            return actualDate;
        }
    
    
        /// <summary>
        ///     Gets or sets the maximum date.
        /// </summary>
        /// <value>The maximum date.</value>
        public DateTime ReturnDate
        {
            get => (DateTime) GetValue(ReturnDateProperty);
            set => SetValue(ReturnDateProperty, value);
        }
    
        #endregion
    
Sign In or Register to comment.