Display tooltip and data labels in release mode in .NET MAUI Cartesian Chart
10 Jul 20265 minutes to read
In SfCartesianChart, tooltip and data label templates do not bind directly to your business model because these elements are generated by the chart at runtime and often represent calculated points. As a result, the chart provides its own binding context containing the necessary metadata. For tooltips this context is TooltipInfo, and for data labels it is ChartDataLabel. Both expose an Item property that points back to your original business model when a direct mapping exists. With .NET 9 compiled bindings, set x:DataType=”chart:TooltipInfo” for tooltip templates and x:DataType=”chart:ChartDataLabel” for data label templates, then bind through Item. If you need a specific field from your model, bind to Item and use a value converter to extract the desired property.
NOTE
Prerequisite: Ensure that the required NuGet package is installed, the necessary namespaces are imported, and the SfCartesianChart control is properly configured in your application. For detailed setup and configuration instructions, refer to the Getting Started guide.
Ahead-of-Time (AOT) compilation converts Intermediate Language to native code at build time to improve startup and runtime performance. AOT commonly ships together with linker trimming in Release configurations to reduce app size.
Because Release builds enable trimming, members referenced only from XAML can be removed unless explicitly preserved. To ensure your bindings work in Release, reference your value converter from XAML and preserve your ViewModel, business model, and converter types. This prevents the linker from removing properties that your tooltip or data label templates access through Item.
<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>
<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:SfCartesianChart.Resources>
<chart:SplineAreaSeries x:Name="series"
ItemsSource="{Binding ChartData}"
XBindingPath="WeekNumber"
YBindingPath="Planned"
EnableTooltip="True"
ShowDataLabels="True"
LabelTemplate="{StaticResource labelTemplate}"
TooltipTemplate="{StaticResource tooltiptemplate}">
</chart:SplineAreaSeries>
</chart:SfCartesianChart>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;
}