Bind the StackGroupKey of the BarSeries when Using ChartSeriesProvider
Environment
Product Version | 2025.2.521 |
Product | RadChartView for WPF |
Description
Binding
the StackGroupKey
property of the BarSeries
when using ChartSeriesProvider
.
Solution
The StackGroupKey
property of the BarSeries
class is not a DependencyProperty
, thus it cannot be bound directly. To bind it when using the ChartSeriesProvider
functionality of RadChartView
, you can implement a custom attached property. You can bind this attached property to a property in the view model of the series. To bind it, you can create a new Style
for the CategoricalSeriesDescriptor
instance.
In order for the
StackGroupKey
property to be taken into account, theCombineMode
property needs to be set to eitherStack
orStack100
.
Adding an additional property for the StackGroupKey in the view model of the series
public class SeriesViewModel
{
//Property that holds the stack group key of the current series
public object StackGroupKey { get; set; }
public ObservableCollection<MyPointInfo> MyPoints { get; set; }
}
Defining the series descriptor and binding the StackGroupKey to an attached property
<telerik:ChartSeriesProvider.SeriesDescriptors>
<telerik:CategoricalSeriesDescriptor ItemsSourcePath="MyPoints"
CategoryPath="Category"
ValuePath="Value">
<telerik:CategoricalSeriesDescriptor.Style>
<Style TargetType="telerik:BarSeries">
<Setter Property="CombineMode" Value="Stack"/>
<Setter Property="local:BarSeriesExtensions.StackGroupKey" Value="{Binding StackGroupKey}"/>
</Style>
</telerik:CategoricalSeriesDescriptor.Style>
</telerik:CategoricalSeriesDescriptor>
</telerik:ChartSeriesProvider.SeriesDescriptors>
Implementing the attached property that will set the StackGroupKey of each BarSeries
public class BarSeriesExtensions
{
public static object GetStackGroupKey(DependencyObject obj)
{
return (object)obj.GetValue(StackGroupKeyProperty);
}
public static void SetStackGroupKey(DependencyObject obj, object value)
{
obj.SetValue(StackGroupKeyProperty, value);
}
public static readonly DependencyProperty StackGroupKeyProperty =
DependencyProperty.RegisterAttached("StackGroupKey", typeof(object), typeof(BarSeriesExtensions), new PropertyMetadata(null, OnStackGroupKeyChanged));
private static void OnStackGroupKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
BarSeries barSeries = (BarSeries)d;
barSeries.StackGroupKey = e.NewValue;
}
}
}