Why Tooltip and DataLabel Are Missing in Release Mode .NET MAUI Chart?
In .NET MAUI SfCartesianChart, tooltip and data label templates use TooltipInfo and ChartDataLabel as their binding context. These context have an Item property, which holds the corresponding business model instance used in the chart’s ItemsSource.
Starting with .NET 9.0, compiled bindings introduced via x:DataType enforce strict type scoping. This means that the tooltip or data label template operates within the scope of TooltipInfo or ChartDataLabel, and cannot directly access properties from your model unless you explicitly bind to the Item property.
To access your model’s properties, you must:
- Set x:DataType=“chart:TooltipInfo” or x:DataType=“chart:ChartDataLabel” in your template.
- Bind to Item and use a value converter to extract the desired property.
Secondly, Release mode builds enable linker trimming and AOT compilation, which can remove members referenced only from XAML. As a result, bindings that work in Debug mode may fail in Release mode unless you explicitly preserve the model and view model members.
To ensure tooltips and data labels display correctly in Release mode, follow these steps:
Step 1: Initialize the SfCartesianChart
Initialize the SfCartesianChart in your .NET MAUI application. For detailed setup instructions, refer to the documentation.
<chart:SfCartesianChart>
<chart:SplineAreaSeries
ItemsSource="{Binding ChartData}"
XBindingPath="WeekNumber"
YBindingPath="Planned">
...
</chart:SplineAreaSeries>
</chart:SfCartesianChart>
Step 2: Create a value converter to read the model from TooltipInfo/ChartDataLabel
This converter extracts the desired property from the Item inside TooltipInfo or ChartDataLabel.
using System.Globalization;
public class ValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is WeekPlan weekPlan ? weekPlan.Planned : value;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => value;
}
Step 3: Define a tooltip template using TooltipInfo scope
Use x:DataType=“chart:TooltipInfo” and bind to Item using the converter and then assign the template to the TooltipTemplate property of the series. This enables the chart to use your custom layout for tooltips.
<chart:SfCartesianChart>
<chart:SfCartesianChart.Resources>
<local:ValueConverter x:Key="valueConverter"/>
<DataTemplate x:Key="tooltiptemplate">
<StackLayout x:DataType="chart:TooltipInfo" Orientation="Vertical">
<Label Text="{Binding Item, Converter={StaticResource valueConverter}, ConverterParameter=Planned, StringFormat='Planned : {0}h'}" TextColor="White"/>
</StackLayout>
</DataTemplate>
</StackLayout>
</DataTemplate>
</chart:SfCartesianChart.Resources>
<chart:SplineAreaSeries x:Name="series" ItemsSource="{Binding ChartData}" XBindingPath="WeekNumber" YBindingPath="Planned" EnableTooltip="True" TooltipTemplate="{StaticResource tooltiptemplate}">
</chart:SfCartesianChart>
Step 4: Define a data label template using ChartDatalabel scope
Define a DataTemplate in Resources with x:DataType=“chart:ChartDataLabel” and bind to Item through the same converter and then assign the template to LabelTemplate and enable ShowDataLabels.
<chart:SplineAreaSeries>
<chart:SplineAreaSeries.DataLabelTemplate>
<DataTemplate x:Key="labelTemplate">
<StackLayout x:DataType="chart:ChartDataLabel" Orientation="Vertical">
<Label Text="{Binding Item, Converter={StaticResource valueConverter}, ConverterParameter=Planned, StringFormat='Label : {0}h'}" TextColor="Black"/>
</StackLayout>
</DataTemplate>
</chart:SplineAreaSeries.DataLabelTemplate>
<chart:SplineAreaSeries x:Name="series" ItemsSource="{Binding ChartData}" XBindingPath="WeekNumber" YBindingPath="Planned" ShowDataLabels="True" LabelTemplate="{StaticResource labelTemplate}" >
</chart:SplineAreaSeries>
Step 5: Preserve your ViewModel and Model classes to prevent trimming
To prevent the linker from trimming unused members in Release mode, apply the [Preserve] attribute.
using System.Collections.ObjectModel;
[Preserve(AllMembers = true)]
public class ViewModel
{
public ObservableCollection<Model> ChartData { get; } = new()
{
new Model { WeekNumber = 1, Planned = 3 },
new Model { WeekNumber = 2, Planned = 5 },
new Model { WeekNumber = 3, Planned = 2 },
new Model { WeekNumber = 4, Planned = 5 },
new Model { WeekNumber = 5, Planned = 7 },
};
}
[Preserve(AllMembers = true)]
public class Model
{
public double WeekNumber { get; set; }
public double Planned { get; set; }
}
Output:
Download the full sample project from GitHub.
Conclusion
I hope you enjoyed learning how to display the missing tooltip and datalabel in release mode .NET MAUI Chart (SfCartesianChart).
You can refer to our .NET MAUI Cartesian Chart’s feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI Chart documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Chart and other .NET MAUI components.
Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!