This is a migrated thread and some comments may be shown as answers.
Get date on CellTapped
11 Answers 48 Views
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 07 Apr 2018, 08:15 PM

In a CellTapped handler, how does one retrieve the date of the tapped cell?

The calendar's selected date will be wrong if the tapped cell become a new selected date.

11 Answers, 1 is accepted

Sort by
0
Ian
Top achievements
Rank 1
answered on 07 Apr 2018, 08:33 PM

I did this...

void Handle_CellTapped(object sender, CalendarCell e)

        {
            if (e is CalendarDateCell dateCell)
            {
                await ShowDayAppointments(dateCell.Date);
            }
        }

Did Xamarin Studio generate the correct signature?

0
Lance | Manager Technical Support
Telerik team
answered on 09 Apr 2018, 02:35 PM
Hi Ian,

CellTapped event args do not provide the date of the cell, because there are many different kinds of cells, not all of them have DateTime objects associated (you can see the type by using the args's Type property).

A better event for your needs is the SelectionChanged event:

private void Calendar_OnSelectionChanged(object sender, ValueChangedEventArgs<object> e)
{
    var lastSelectedDate = (DateTime)e.PreviousValue;
    var currentlySelectedDate = (DateTime)e.NewValue;
}


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ian
Top achievements
Rank 1
answered on 09 Apr 2018, 03:11 PM
But SelectionChanged only fires once when you click on a cell twice in a row.
0
Lance | Manager Technical Support
Telerik team
answered on 09 Apr 2018, 04:48 PM
Hello Ian,

In this case, you can just read the calendar's SelectedDate property.  For example, you can make sure that the user tapped the cell type that you only want to get the date from, then use the value of SelectedDate:

private void Calendar_OnCellTapped(object sender, CalendarCell e)
{
    if (e.Type == CalendarCellType.Date)
    {
        var date = calendar.SelectedDate;
    }
}


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ian
Top achievements
Rank 1
answered on 09 Apr 2018, 07:00 PM

But if the tapped cell is a new cell, the SelectedDate is out of date because its value is updated after OnCellTapped.

I mentioned this in my original post.

0
Lance | Manager Technical Support
Telerik team
answered on 09 Apr 2018, 08:08 PM
Hello Ian,

Correct, this is why we don't recommend using CellTapped for data requirements, it's designed use is not for determining the date/time of the cell (it's used for many types of cells, not just cell associated with a DateTime). 

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Lance | Manager Technical Support
Telerik team
answered on 09 Apr 2018, 08:26 PM
Hi Ian,

I've created an experimental approach that you can try that will give you the tapped cell's date (regardless how how many times it is tapped). This uses the cell's text to create a DateTime instance.

private void Calendar_OnCellTapped(object sender, CalendarCell e)
{
    if (e.Type == CalendarCellType.Date)
    {
        var computedDate = new DateTime(
            calendar.DisplayDate.Year,
            calendar.DisplayDate.Month,
            Convert.ToInt32(e.Text));
 
        Debug.WriteLine($"CellTapped - EstimatedDate of Tapped Cell: {computedDate}");
    }
}

Here's my output after testing with two cells in Month view:

CellTapped - EstimatedDate of Tapped Cell: 4/10/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/10/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/10/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/11/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/11/2018 12:00:00 AM


I urge that you test this thoroughly before using it in an application, this is not the designed used of the event and we don't recommend it for production. It works in Month view as-is, but I would add a bit of defensive programming for when the text can't be converted to Int, if the combination doesn't convert to a valid DateTime, etc.

I hope this helps.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ian
Top achievements
Rank 1
answered on 09 Apr 2018, 08:28 PM
Thanks. Although I prefer my method from my second post. It checks whether a tapped cell is a CalendarDateCell, which has a date property.
0
Lance | Manager Technical Support
Telerik team
answered on 10 Apr 2018, 02:58 PM
Hello Ian,

I agree that the original approach is the best option. If you have any further issues, please share the problematic code and I'll be happy to assist.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
n/a
Top achievements
Rank 1
answered on 01 Oct 2019, 08:30 AM
The only issue with this is the date works on a single tap but if you tap on the grey dates at the bottom which are the next month it reads as the month you are currently viewing
0
Lance | Manager Technical Support
Telerik team
answered on 02 Oct 2019, 05:08 PM

Hi Matthew,

This is expected, the CellTapped event is not intended for date value selection. Unless you have a special custom condition, we strongly recommend using the built-in selection mechanism. See https://docs.telerik.com/devtools/xamarin/controls/calendar/calendar-selection 

 

If you want to bypass the selection mechanism, this is not something we support, but is achievable with enough custom logic.

For example, using your scenario, you need a few layers of checks for all the properties of a cell. If it is a disabled cell, then try reading the text in that cell to see if it's between 1 and 7 or between 23 and 31. If you know that, then you can make a logical branch that adds or subtracts a month from the current date's month

As you can see this is a lot of custom logic to work around what we already have built-in. Ian has a special case that made it worth doing this.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Asked by
Ian
Top achievements
Rank 1
Answers by
Ian
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
n/a
Top achievements
Rank 1
Share this question
or