How to Apply OS Level Theme in .NET MAUI Applications for DataGrid?
In this article, we will show you how to apply an OS level theme in .NET MAUI applications for the .NET MAUI DataGrid.
C#
The code below sets the visual theme of the application based on the system’s light or dark mode. It listens for theme changes and applies the appropriate Syncfusion® visual theme accordingly when the app appears or the system theme changes.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
if (Application.Current != null)
{
this.ApplyTheme(Application.Current.RequestedTheme);
Application.Current.RequestedThemeChanged += OnRequestedThemeChanged;
}
base.OnAppearing();
}
private void OnRequestedThemeChanged(object? sender, AppThemeChangedEventArgs e)
{
this.ApplyTheme(e.RequestedTheme);
}
public void ApplyTheme(AppTheme appTheme)
{
if (Application.Current != null)
{
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
var syncTheme = mergedDictionaries.OfType<SyncfusionThemeResourceDictionary>().FirstOrDefault();
if (syncTheme != null)
{
if (appTheme is AppTheme.Light)
{
syncTheme.VisualTheme = SfVisuals.MaterialLight;
}
else
{
syncTheme.VisualTheme = SfVisuals.MaterialDark;
}
}
}
}
}
}
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to apply an OS level theme in .NET MAUI applications for DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!