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. (Last updated on: November 16, 2018).
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: UWP |
Control: SfChart |
Published Date: March 4, 2016 |
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.

C#

    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);
            } 
        }
    }

XAML

<Window x:Class="Annotation_DataContext.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:chart="http://schemas.syncfusion.com/wpf"
        xmlns:local="clr-namespace:Annotation_DataContext"
        Title="Annotation DataContext" Height="700" Width="1000">
    <Window.Resources>
        <DataTemplate x:Key="annotationTemplate1">
            <TextBlock Text="{Binding Path=Annotation1}" Background="LightPink" Foreground="Black" FontSize="15" FontWeight="Medium"/>
        </DataTemplate>
        <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"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
            <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.DataContext>
                <local:ViewModel/>
            </Grid.DataContext>
            <chart:SfChart x:Name="chart" Header="PRODUCT DETAILS" FontSize="18" FontWeight="ExtraBlack">
            <!--create custom text annotation-->
            <chart:SfChart.Annotations>
                <local:CustomTextAnnotation X1="2" Y1="250" Content="{Binding}" ContentTemplate="{StaticResource annotationTemplate1}"/>
                <local:CustomTextAnnotation X1="0" Y1="150" Content="{Binding}" ContentTemplate="{StaticResource annotationTemplate2}"/>
            </chart:SfChart.Annotations>
                <chart:SfChart.PrimaryAxis>
                    <chart:CategoryAxis/>
                </chart:SfChart.PrimaryAxis>
                <chart:SfChart.SecondaryAxis>
                    <chart:NumericalAxis Interval="50"/>
                </chart:SfChart.SecondaryAxis>
            <chart:ColumnSeries ItemsSource="{Binding Products}" Palette="Metro" XBindingPath="Name" YBindingPath="Range">
                </chart:ColumnSeries>
            </chart:SfChart>
    </Grid>
</Window>

Output

Chart with custom annotations binding with the underlaying values in WPF

2X faster development

The ultimate UWP UI toolkit to boost your development speed.
ADD COMMENT
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