I want to show a custom cell view if the cell's date is the current date (inside a circle). And when the user taps on a cell, it should change the border colour of the circle. I tried it using calendar_OnMonthCellLoaded event. I was able to customize the cell view. But after that, the selected cell highlight feature was disabled.
So I went to try It using a converter.
<DataTrigger
TargetType="border:SfBorder" Binding="{Binding Day, Converter={StaticResource IsTodayConverter},ConverterParameter={Binding Date}}"
Value="false">
<Setter Property="BorderColor" Value="Transparent"/>
<Setter Property="BorderWidth" Value="0"/>
</DataTrigger>
The converter receives the date as an integer. But, it does not pass the whole datetime object.
Here is my converter.
public class IsTodayConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool result = false;
try
{
if (value is DateTime day)
{
if (day.ToString("ddMMyyyy") == DateTime.Now.ToString("ddMMyyyy"))
{
result = true;
}
}
}
catch (Exception ex)
{
}
return result;
}
So, I want to know how to do it.
|
<calendar:MonthViewSettings.CellTemplate>
<DataTemplate>
<AbsoluteLayout BackgroundColor="Transparent"
Margin="0"
Padding="0">
<Frame IsVisible="{Binding IsSelected}"
BackgroundColor="Transparent"
BorderColor="{Binding BorderColor}"
HasShadow="false"
AbsoluteLayout.LayoutBounds="0.5,0.5,30,30"
AbsoluteLayout.LayoutFlags="PositionProportional"
CornerRadius="15"
HeightRequest="30"
WidthRequest="30"
VerticalOptions="Center"
HorizontalOptions="Center">
</Frame>
<Label FontSize="13"
FontAttributes="None"
LineBreakMode="TailTruncation"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="0.5,0.5,1,1"
AbsoluteLayout.LayoutFlags="All"
Text="{Binding DayNumber}"
TextColor="{Binding DateTextColor}"
/>
</AbsoluteLayout>
</DataTemplate>
</calendar:MonthViewSettings.CellTemplate>
private void Calendar_OnMonthCellLoaded(object sender, MonthCellLoadedEventArgs e)
{
var model = new ViewModel();
model.DayNumber = e.Date.Day.ToString();
if (Convert.ToDateTime(calendar.SelectedDate).Day == e.Date.Day && Convert.ToDateTime(calendar.SelectedDate).Month == e.Date.Month && Convert.ToDateTime(calendar.SelectedDate).Year == e.Date.Year)
{
model.IsSelected = true;
if (e.Date.Date == DateTime.Now.Date)
{
model.BorderColor = Color.Orange;
}
}
e.CellBindingContext = model;
} |
Thank you!
I was able to solve it by extracting the cell view to a custom template view and modify its properties dynamically on
calendar_OnMonthCellLoaded