Pie chart inside a ContentControl is not displaying correctly
Attached is a simple sample code. Three pie charts are created on the main window. I would like to ask why one of the pie charts, which is placed inside a ContentControl and has its Series porperty binding, does not render properly?
Attachment: PieChartDemo_b401037e.zip
Hi ufolbb,
We have validated your query with the attached sample. We suspect that the issue may be related to the sequence in which the visual tree and the template are applied. Our development team is actively investigating this further for additional validation.
For now, we recommend binding the entire chart from the ViewModel and using it within a ContentControl. This approach will make it easier to manage your business logic from the UI.
C#:
|
private SfChart _chart;
public SfChart Chart { get { return _chart; } set { _chart = value; OnPropertyChanged(); } } public MainWindowViewModel() { Chart = new SfChart(); Chart.Series = new ChartSeriesCollection { new PieSeries { ItemsSource = ChartValues, XBindingPath = "Name", YBindingPath = "Value", SegmentColorPath = "Color", EnableAnimation = true, Stroke = new SolidColorBrush(Colors.White), StrokeThickness = 1, ListenPropertyChange = true, ShowTooltip = true } }; } |
XML:
|
<DataTemplate x:Key="ChartTemplate"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <ContentControl Margin="10" Width="200" Height="200" Content="{Binding Chart}"/> <chart:SfChart Margin="10" Width="200" Height="200"> <chart:PieSeries ShowTooltip="True" ItemsSource="{Binding ChartValues}" YBindingPath="Value" XBindingPath="Name" SegmentColorPath="Color"> </chart:PieSeries> </chart:SfChart> </StackPanel> </DataTemplate>
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ContentControl Grid.Row="1" Content="{Binding}" ContentTemplate="{StaticResource ChartTemplate}" > </ContentControl> </Grid>
|
Demo:
Please review the above suggestion and let us know if it meets your use case. If you need any further assistance, we will be happy to help.
Regards,
Baranibharathi P.
Attachment: SyncfusionDemo_29c27f3a.zip
This solution can solve the problem, but in my real project, a large amount of code and styles are implemented in XAML, and this would require changing quite a lot of code. Is there any other workaround?
Hi ufolbb,
We are currently analyzing the scenario where the series is not being rendered when added through a ContentControl, specifically when binding the Series collection from the ViewModel.
During our investigation, we identified that the BindingExpression associated with the Series property appears to be breaking unexpectedly. We are actively working to determine the root cause and understand the underlying behavior.
Our team will continue the investigation and share detailed findings within the next two business days(13/06/2026).
We appreciate your patience and understanding.
Regards,
Dhanaraj R.
Hi ufolbb,
Thank you for your patience while we investigated this behavior.
The problem occurs due to binding timing when the chart is placed inside a ContentControl with a DataTemplate. In this case, the Series binding is not yet resolved when the dependency property callback executes, causing it to receive a null value. Because of this, an internal condition fails and the required event listener is not registered. Even though the binding resolves later, the initialization step is not retriggered, so the chart does not render correctly.
As a workaround, we recommend deferring the assignment of the Series property until the visual tree is fully loaded using an attached behavior.
|
public static class ChartSeriesBehavior { public static ChartSeriesCollection GetSeriesCollection(DependencyObject obj) { return (ChartSeriesCollection)obj.GetValue(SeriesCollectionProperty); }
public static void SetSeriesCollection(DependencyObject obj, ChartSeriesCollection value) { obj.SetValue(SeriesCollectionProperty, value); }
public static readonly DependencyProperty SeriesCollectionProperty = DependencyProperty.RegisterAttached( "SeriesCollection", typeof(ChartSeriesCollection), typeof(ChartSeriesBehavior), new PropertyMetadata(null, OnSeriesCollectionChanged));
private static void OnSeriesCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is SfChart chart && e.NewValue is ChartSeriesCollection seriesCollection) { chart.Series.Clear(); foreach (var series in seriesCollection) { chart.Series.Add(series); } } } } |
|
<syncfusion:SfChart local:ChartSeriesBehavior.SeriesCollection="{Binding TestCollection}"> </syncfusion:SfChart> |
This ensures the binding is resolved before the property is applied, allowing proper initialization and rendering without requiring major changes to your existing XAML structure.
Output:
We hope this helps. If you need further assistance, don’t hesitate to ask out.
Regards,
Baranibharathi P.
Attachment: SyncfusionDemo_3ece1eb6.zip
Thank you, this solution has been very helpful in solving my issue.
Hi ufolbb,
You are welcome.
We are glad that the provided response meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.
Regards,
Preethi R