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

Set PieSeries SliceStyles from (static)resource.

2 Answers 59 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Dipak
Top achievements
Rank 1
Dipak asked on 11 Feb 2014, 04:10 PM
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 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 14 Feb 2014, 09:40 AM
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).

0
Dipak
Top achievements
Rank 1
answered on 14 Feb 2014, 10:24 AM
Thanks Martin for your guidance,

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

Regards
Dipak.
Tags
Chart
Asked by
Dipak
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Dipak
Top achievements
Rank 1
Share this question
or