We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind the ViewModel property to content template of a TextAnnotation?

Platform: Windows Phone |
Control: SfChart |
Published Date: November 13, 2014 |
Last Revised Date: June 27, 2019

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">


Chart with custom annotations binding with the underlaying values in WPF

You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon