Set PieSeries SliceStyles from (static)resource.

3 posts, 0 answers
  1. Dipak
    Dipak avatar
    2 posts
    Member since:
    Feb 2014

    Posted 11 Feb 2014 Link to this post

    Hi there,

    I would like to share my style resource across multiple chart controls, But I am unable to do this as "SliceStyles" hasn't got setter.

    In short I want to assign following resource to SliceStyles.
      <x:Array Type="Style" x:Key="styleList">
            <Style TargetType="Path">
                <Setter Property="Fill" Value="#FF5661"/>
            </Style>
            <Style TargetType="Path">
                <Setter Property="Fill" Value="#FF56DA"/>
            </Style>
             .......


    .......
            <Style TargetType="Path">             <Setter Property="Fill" Value="#89638E"/>         </Style>     </x:Array>

    So far I tried below code, which will not work, Is there any workaround OR How should we address this in best way.
     <telerik:PieSeries ItemsSource="{Binding Items}" SliceStyles="{Binding Path=., Source={StaticResource styleList}}"
     .....

    Note: We do not want to do following.
      <telerik:PieSeries.SliceStyles>
             <Style TargetType="Path">
                  <Setter Property="Fill" Value="#FF5661"/>
             </Style>
    .......
            <Style TargetType="Path">             <Setter Property="Fill" Value="#89638E"/>         </Style>
      </telerik:PieSeries.SliceStyles>
    
    
  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 14 Feb 2014 Link to this post

    Hi Dipak,

    If you only want to change the color of the Pie slices the better approach would be to use a Palette. But if you want to change another style property, for example the Stroke, you should use the SliceStyles collection. As you have noticed this collection is read-only and you cannot assign it directly.

    However you could workaround this behavior and achieve your requirement by using an attached property where you could add the styles to the current SliceStyles collection. Here is a sample in code:
    public static ICollection GetSliceStyles(DependencyObject obj)
    {
        return (ICollection)obj.GetValue(SliceStylesProperty);
    }
     
    public static void SetSliceStyles(DependencyObject obj, int value)
    {
        obj.SetValue(SliceStylesProperty, value);
    }
     
    public static readonly DependencyProperty SliceStylesProperty =
        DependencyProperty.RegisterAttached("SliceStyles", typeof(ICollection), typeof(ChartViewUtilities), new PropertyMetadata(null, AddSliceStyles));
     
    private static void AddSliceStyles(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PieSeries pieSeries = (PieSeries)d;
        ICollection styles = (ICollection)e.NewValue;
     
        foreach (Style style in styles)
        {
            pieSeries.SliceStyles.Add(style);
        }
    }

    I attached a project demonstrating how to change the colors with predefined palette, custom palette and an attached property.

    Regards,
    Martin
    Telerik

    Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

  3. UI for WPF is Visual Studio 2017 Ready
  4. Dipak
    Dipak avatar
    2 posts
    Member since:
    Feb 2014

    Posted 14 Feb 2014 in reply to Martin Link to this post

    Thanks Martin for your guidance,

    Attached prop/behaviour are always been last resort in WPF nasty stuff.

    Regards
    Dipak.
Back to Top