|
| 1 | +--- |
| 2 | +title: Displaying Current Selection State in Calendar for UI for .NET MAUI |
| 3 | +description: Learn how to display the current selection state (day, month, or year) in Calendar for UI for .NET MAUI using the DisplayMode property and a custom converter. |
| 4 | +type: how-to |
| 5 | +page_title: How to Identify Current Selection State in .NET MAUI Calendar |
| 6 | +meta_title: How to Identify Current Selection State in .NET MAUI Calendar |
| 7 | +slug: identify-current-selection-state-calendar-net-maui |
| 8 | +tags: calendar, ui-for-dotnet-maui, displaymode, converter, semanticproperties, description, headerlabelstyle |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | --- | |
| 16 | +| 11.1.0 | Calendar for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I want to determine the current selection state in the Calendar for UI for .NET MAUI, such as day, month, or year, to set a different `SemanticProperties.Description` dynamically based on the selection state. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to bind Calendar's DisplayMode property to update UI elements? |
| 24 | +- How to use converters to adjust properties dynamically in .NET MAUI? |
| 25 | +- How to apply styles dynamically based on Calendar selection state? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To achieve this, bind the `DisplayMode` property of the Calendar to the `SemanticProperties.Description` of the header text using the `HeaderLabelStyle` property. Use a custom converter to transform the `DisplayMode` value into the desired format. |
| 30 | + |
| 31 | +1. Define a custom converter to handle the transformation of the `DisplayMode` value. |
| 32 | + |
| 33 | +```csharp |
| 34 | +public class MyConverter : IValueConverter |
| 35 | +{ |
| 36 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 37 | + { |
| 38 | + return value.ToString(); // Convert the DisplayMode to a string representation. |
| 39 | + } |
| 40 | + |
| 41 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 42 | + { |
| 43 | + throw new NotImplementedException(); // Not needed for this use case. |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | +2. Create and apply a style for the header label using the `HeaderLabelStyle` property. |
| 48 | +3. Bind the `SemanticProperties.Description` to the Calendar's `DisplayMode` property, applying the custom converter. |
| 49 | + |
| 50 | + |
| 51 | +```xml |
| 52 | +<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
| 53 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 54 | + xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui" |
| 55 | + xmlns:local="clr-namespace:MauiApp7" |
| 56 | + x:Class="MauiApp7.MainPage"> |
| 57 | + |
| 58 | + <ContentPage.Resources> |
| 59 | + <ResourceDictionary> |
| 60 | + <local:MyConverter x:Key="myConverter" /> |
| 61 | + <Style TargetType="Label" x:Key="HeaderLabelStyle"> |
| 62 | + <Setter Property="TextColor" Value="#8660C5" /> |
| 63 | + <Setter Property="FontSize" Value="20" /> |
| 64 | + <Setter Property="FontAttributes" Value="Italic" /> |
| 65 | + <Setter Property="SemanticProperties.Description" Value="{Binding Source={x:Reference calendar}, Path=DisplayMode, Converter={StaticResource myConverter}}"/> |
| 66 | + </Style> |
| 67 | + </ResourceDictionary> |
| 68 | + </ContentPage.Resources> |
| 69 | + |
| 70 | + <telerik:RadCalendar x:Name="calendar" |
| 71 | + HeaderLabelStyle="{StaticResource HeaderLabelStyle}" /> |
| 72 | +</ContentPage> |
| 73 | +``` |
| 74 | + |
| 75 | +## See Also |
| 76 | + |
| 77 | +- [Calendar for UI for .NET MAUI Overview](https://www.telerik.com/maui-ui/documentation/controls/calendar/overview) |
| 78 | +- [UI for .NET MAUI DisplayMode Property Documentation](https://www.telerik.com/maui-ui/documentation/controls/calendar/display-modes) |
0 commit comments