This is a migrated thread and some comments may be shown as answers.

How to creatre Custom tooltip template from code behind?

3 Answers 448 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chinmaya
Top achievements
Rank 1
Chinmaya asked on 27 Aug 2014, 02:26 PM
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;

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 01 Sep 2014, 10:45 AM
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.
 
0
Chinmaya
Top achievements
Rank 1
answered on 02 Sep 2014, 03:04 PM
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
0
Martin Ivanov
Telerik team
answered on 02 Sep 2014, 03:17 PM
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.
 
Tags
ChartView
Asked by
Chinmaya
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Chinmaya
Top achievements
Rank 1
Share this question
or