How to creatre Custom tooltip template from code behind?

4 posts, 0 answers
  1. Chinmaya
    Chinmaya avatar
    24 posts
    Member since:
    Aug 2014

    Posted 27 Aug 2014 Link to this post

    Curently i am creating a default tooltip for bar series .. I want to create a custom tooltip template and then use it with this barchart. I am totally new to C# and silverlight .

    CategoricalAxis xAxis = new CategoricalAxis();
                    xAxis.LabelFitMode = AxisLabelFitMode.Rotate;
                    LinearAxis y1Axis = new LinearAxis();
                    y1Axis.RangeExtendDirection = Telerik.Charting.NumericalAxisRangeExtendDirection.None;             
                    chart.HorizontalAxis = xAxis;
                    chart.VerticalAxis = y1Axis;
                    xAxis.LabelFitMode = AxisLabelFitMode.Rotate;

    //current tooltip
                     chart.Behaviors.Add(new ChartTooltipBehavior() { Placement = PlacementMode.Top });

    BarSeries okSeries = new BarSeries();
                    okSeries.ItemsSource = this.yields;
                    okSeries.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "LocationName" };
                    okSeries.ValueBinding = new GenericDataPointBinding<Yield, int?>() { ValueSelector = yield => yield.OK };
                    okSeries.CombineMode = ChartSeriesCombineMode.Stack;
                    okSeries.DefaultVisualStyle = App.Current.Resources["YieldOK"] as Style;
                
                 
                    BarSeries nokSeries = new BarSeries();
                    nokSeries.ItemsSource = this.yields;
                    nokSeries.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "LocationName" };
                    nokSeries.ValueBinding = new GenericDataPointBinding<Yield, int?>() { ValueSelector = yield => yield.NOK };
                    nokSeries.CombineMode = ChartSeriesCombineMode.Stack;
                    nokSeries.DefaultVisualStyle = App.Current.Resources["YieldNOK"] as Style;
  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 01 Sep 2014 Link to this post

    Hello Chinmaya,

    In order to define a template for the tool tip behavior of the RadCartesianChart you can use its TooltipTemplate property. As for the creating of the DataTemplate I suggest you to define it in xaml and then get it by its key. Here is an example:

    chart.TooltipTemplate = this.Resources["myToolTipTemplate"] as DataTemplate;

    <DataTemplate x:Key="myToolTipTemplate">          
        <TextBlock Text="I am a tool tip" />
    </DataTemplate>

    I hope this helps.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
  4. Chinmaya
    Chinmaya avatar
    24 posts
    Member since:
    Aug 2014

    Posted 02 Sep 2014 in reply to Martin Link to this post

    Hello Martin,

    Thank you, now its working but i had one more question

    can we set below code from code behind.?
     
    <DataTemplate x:Key="myToolTipTemplate">          
        <TextBlock Text="I am a tool tip" />
    </DataTemplate>

    best regards,
    Chinmaya
  5. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 02 Sep 2014 Link to this post

    Hi Chinmaya,

    You can create an instance of the DataTemplate class and add the elements you want inside of it, but this approach is deprecated and it is not recommended. If you want to define a DataTemplate in code-behind I suggest you to use a string or a stream containing the template and load it with the XamlReader.Load() method. 

    Basically you can use something like the following:
    var templateString = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><TextBlock Text=""I am a tool tip"" /></DataTemplate>";
    var labelTemplate = XamlReader.Load(templateString) as DataTemplate;
    chart.TooltipTemplate = labelTemplate;

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top