Changing DataTemplate of Bar Series

3 posts, 1 answers
  1. Dts
    Dts avatar
    12 posts
    Member since:
    Apr 2013

    Posted 10 Apr 2013 Link to this post

    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. Answer
    Missing user
    Missing user avatar

    Posted 15 Apr 2013 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Dts
    Dts avatar
    12 posts
    Member since:
    Apr 2013

    Posted 15 Apr 2013 Link to this post

    You have given me a great solution.It's working properly according to my need.
    Thanks a lot!

     
Back to Top