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 |
This page will automatically be redirected to the sign-in page in 10 seconds.