Pre loader

SciChart export to Png Exception in MVVM: Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

1
0

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!

Version
4
  • You must to post comments
0
0

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.

  • You must to post comments
0
0

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

  • Ehsan Masnavi
    Hello, I am using version 4.2.5.10565
  • Andrew Burnett-Thompson
    Have you tried v5? If the bug exists there we will investigate, however I think it has been fixed.
  • charlesZha Yong
    Excuse me,I met the same trouble “Element already has a logical parent. It must be detached “. Version=5.0.0.10963, Culture=neutral, PublicKeyToken=b55dd9efe817e823.If you can,please reply as soon as possible. Have a good day.
  • Andrew Burnett-Thompson
    Looking at the original code sample above, my guess is the copy chart AppSciChart created in SaveChart() uses the same YAxes as the original chart, hence you have the error: an element already has a logical parent. This won’t work. In WPF you can only have one element with one parent object. SciChart already has a mechanism to export a chart to a different size, and it is possible to change theme before export. I would suggest either using our built in mechanism to export OR ensuring that there are no axis with two parent charts before export. If either of you are still stuck and require assistance, send some code to reproduce to our support desk and we will help. Best regards, Andrew
  • You must to post comments
0
0

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.

Attachments
  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies