Articles in this section
Category / Section

How to bind the content template of text annotation in WPF Chart?

2 mins read

WPF Chart (SfChart) allows you to customize the annotation’s behavior by defining your own template using the ContentTemplate property. In some scenarios, you may want to display or use underlying values or any other values from any object.

In this case, you need the specific object in your template, since by default data context of the content template has string value. You can customize this to get the data context as object. For this requirement, you should define the custom annotation inheritance from TextAnnotation as shown in the following code example.


public class CustomTextAnnotation : TextAnnotation
  public object Content
    get { return (object)GetValue(ContentProperty); }
    set { SetValue(ContentProperty, value); }
  // Using DependencyProperty as the backing store for TextString.  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(CustomTextAnnotation), new PropertyMetadata(null));
  protected override void SetBindings()
    base.SetBindings();  //You need to invoke this method to make all the default bindings
    if (TextElement != null)
      Binding textBinding = new Binding { Path = new PropertyPath("Content"), Source = this };
      //TextElement is content control define internally to display text.
      TextElement.SetBinding(ContentControl.ContentProperty, textBinding);


<Window x:Class="Annotation_DataContext.MainWindow"
        Title="Annotation DataContext" Height="700" Width="1000">
        <DataTemplate x:Key="annotationTemplate1">
            <TextBlock Text="{Binding Path=Annotation1}" Background="LightPink" Foreground="Black" FontSize="15" FontWeight="Medium"/>
        <DataTemplate x:Key="annotationTemplate2">
            <StackPanel HorizontalAlignment="Center">
                <TextBlock Text="{Binding Path=Annotation2.Name}" Background="LightPink"  Foreground="Black" FontSize="15" FontWeight="Medium" Width="100"/>
                <TextBlock Text="{Binding Path=Annotation2.Range}" Background="LightPink"  Foreground="Black" FontSize="15" FontWeight="Medium" Width="100"/>
            <chart:SfChart x:Name="chart" Header="PRODUCT DETAILS" FontSize="18" FontWeight="ExtraBlack">
            <!--create custom text annotation-->
                <local:CustomTextAnnotation X1="2" Y1="250" Content="{Binding}" ContentTemplate="{StaticResource annotationTemplate1}"/>
                <local:CustomTextAnnotation X1="0" Y1="150" Content="{Binding}" ContentTemplate="{StaticResource annotationTemplate2}"/>
                    <chart:NumericalAxis Interval="50"/>
            <chart:ColumnSeries ItemsSource="{Binding Products}" Palette="Metro" XBindingPath="Name" YBindingPath="Range">


WPF Chart with Custom Annotations


I hope you enjoyed learning about how to bind the content template of TextAnnotation in WPF Chart (SfChart).

You can refer to our  WPF Chart feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Chart documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment