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

Changing DataTemplate of Bar Series

2 Answers 140 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Dts
Top achievements
Rank 1
Dts asked on 10 Apr 2013, 01:08 PM
Hi,
    I am creating application in that we have multiple series representing different data source just like line,bar,etc.Now what i want to accomplish is that i want to change the data template of bar series so that each rectangle will have of different color.I found this in Online documentation but it is accomplished in XAML. I did not find any resource of changing the template using C# code.Could you please help me to accomplish this?
Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Accepted
Missing User
answered on 15 Apr 2013, 08:02 AM
Hi,

I assume the resource from our online documentation you referred to is this one.

In C# you can simply manually create the data templates from code-behind and add them to the PointTemplates collection of the BarSeries. So instead of adding them in XAML like this:
<telerik:BarSeries Name="barSeries">
    <telerik:BarSeries.PointTemplates>
        <!-- ... -->
    </telerik:BarSeries.PointTemplates>
</telerik:BarSeries />
A similar effect can be achieved in code-behind like this:
DataTemplate dt1 = new DataTemplate { DataType = typeof(Rectangle) };
FrameworkElementFactory dt1Factory = new FrameworkElementFactory { Type = typeof(Rectangle) };
dt1Factory.SetValue(Rectangle.FillProperty, new SolidColorBrush(Colors.Red));
dt1.VisualTree = dt1Factory;
 
DataTemplate dt2 = new DataTemplate { DataType = typeof(Rectangle) };
FrameworkElementFactory dt2Factory = new FrameworkElementFactory { Type = typeof(Rectangle) };
dt2Factory.SetValue(Rectangle.FillProperty, new SolidColorBrush(Colors.Green));
dt2.VisualTree = dt2Factory;
 
DataTemplate dt3 = new DataTemplate { DataType = typeof(Rectangle) };
FrameworkElementFactory dt3Factory = new FrameworkElementFactory { Type = typeof(Rectangle) };
dt3Factory.SetValue(Rectangle.FillProperty, new SolidColorBrush(Colors.Blue));
dt3.VisualTree = dt3Factory;
 
this.barSeries.PointTemplates.Add(dt1);
this.barSeries.PointTemplates.Add(dt2);
this.barSeries.PointTemplates.Add(dt3);

Let me know if this helps.

Greetings,
Ivan N.
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dts
Top achievements
Rank 1
answered on 15 Apr 2013, 08:20 AM
You have given me a great solution.It's working properly according to my need.
Thanks a lot!

 
Tags
ChartView
Asked by
Dts
Top achievements
Rank 1
Answers by
Missing User
Dts
Top achievements
Rank 1
Share this question
or