SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
Hi all,
I am trying to save a copy of my chart in form of a “Png” image. However I keep getting the following exception:
“Element already has a logical parent. It must be detached from the old parent before it is attached to a new one”
I was wondering if anyone could help me passing through this.
Here is what I do:
I have a main chart surface in my program of which properties are bound to my ViewModel. For instance, as can be seen from the snippet below, my SciChartSurface’s YAxes is bound to an AxisCollection that resides in my ViewModel. Same thing is done for the RenderableSeries, Annotations, ChartModifierGroup, ChartVisibility and the XAxis properties of my Chart Surface:
<sci:SciChartSurface Grid.Row="1" Grid.Column="0"
RenderableSeries="{Binding SciChartSeriesViewModels}"
Padding="0,8,0,2"
sci:ThemeManager.Theme="BrightSpark"
YAxes="{Binding SciChartYAxesCollection, Mode=TwoWay}"
AutoRangeOnStartup="True"
Annotations="{Binding ChartAnnotations}"
x:Name="ApplicationSciChart"
ChartModifier ="{Binding ChartModifierGroup}"
BorderBrush="LightSlateGray"
BorderThickness="1"
Visibility="{Binding ChartVisibility}"
XAxis="{Binding SciChartXAxis}">
</sci:SciChartSurface>
Now, In order to save a copy of my chart into a “Png” file, I created a new SciChartsurface in my Viewmodel. This newly created chart is desired to be rendered in the memory so I can save it as an Image (Very similar to what is instructed in: Screenshots, Printing and Export to XPS Traingin module. Somewhere in my ViewModel, after I generated all the required data for creating my chart, I call a function to form a new SciChart (rendered in memory) and perform the saving action:
private void SaveChart()
{
SciChartSurface AppSciChart = new SciChartSurface()
{
RenderableSeries = SciChartSeriesViewModels,
XAxis = new TimeSpanAxis(),
YAxes = SciChartYAxesCollection,
ChartTitle = "Rendered in memory",
Annotations = ChartAnnotations
};
var parent = AppSciChart.Parent;
AppSciChart.Width = 1920;
AppSciChart.Height = 1080;
ThemeManager.SetTheme(AppSciChart, "BrightSpark");
AppSciChart.ExportToFile("C:\\Chart.png",SciChart.Core.ExportType.Png, false);
}
The RenderableSeries, YAxis and Annotations of the Chart Surface in the above code (AppSciChart) are the same as the ones I used for my main chart in my XAML code (First Snippet above).
My main chart surface in my application shows up fine, and performs what it is supposed to, but as soon as the SaveChart() is called, the program stops with an Unhandled Exception: System.InvalidOperationException: ‘Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.’
I was wondering if anybody could let me know what I am possibly missing here!
Many thanks!
Hi Ehsan
This sounds like a bug that was fixed a long time ago. Are you using the latest version of SciChart? If not, does the bug occur in the latest version?
Best regards,
Andrew
Hi,
I encounter the same ‘Element already has a logical parent…’ problem. I have defined a SciChartSurface with bindings to RenderableSeriesViewModels and it’s NumericAxis with some property bindings all in XAML (No surface or axis gets created in code). If I navigate to the ViewModel with my ChartSurface for the first time everything is fine but if I navigate to another ViewModel and then back again this exception is thrown.
Any ideas whats going wrong?
Best regards,
Roland
I’m using v5.3.0.11954
The file attached is only a snippet.
Hello Ronald D,
Can you provide more info about how you’re performing export, or better prepare and send for us some small sample application so we can investigate it and find a best solution for you.
Please login first to submit.