We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Binding the same observable collection to two different SFCharts causes a - Specified element is already the logical child of another element

Thread ID:

Created:

Updated:

Platform:

Replies:

132821 Sep 22,2017 03:43 PM UTC Oct 4,2017 01:26 PM UTC WPF 7
loading
Tags: SfChart
Matthew Hewitt
Asked On September 22, 2017 03:43 PM UTC

Hi,

So I am trying to bind the same annotations to two seperate SFCharts using the binding below

Annotations="{Binding ChartAnnotation}"

in the definintion of my SFChart. This works perfectly fine when we only do it once but when we attempt to do it for the second graph it throws this error.

The binding is pointing to this

        private AnnotationCollection _ChartAnnotation = new AnnotationCollection();
        public AnnotationCollection ChartAnnotation
        {
            get
            {
                return _ChartAnnotation;
            }
            set
            {
                _ChartAnnotation = value;
                NotifyPropertyChanged();
            }
        }


Anyone have any idea what could be causing this.

Many Thanks


Durgadevi Selvaraj [Syncfusion]
Replied On September 25, 2017 12:18 PM UTC

Hi Matthew, 
Thanks for contacting Syncfusion Support. 
We have analyzed your  query and in your sample you have used both chart as a child of Grid control with same data context object. So, we are unable to add same data context object element to more than one control in same visual tree. we can resolve this problem by setting data context as separate to the visual tree element(chart) as like in the below code, 
MainWindow.xaml: 
<Grid.Resources> 
     <local:ViewModel x:Key="view" x:Shared="false"/> 
 </Grid.Resources>   
 
<chart:SfChart Margin="10"  Annotations="{Binding Annotations}" DataContext="{StaticResource view}"  > 
           
            <chart:SfChart.PrimaryAxis> 
                <chart:CategoryAxis/> 
            </chart:SfChart.PrimaryAxis> 
            <chart:SfChart.SecondaryAxis> 
                <chart:NumericalAxis/> 
            </chart:SfChart.SecondaryAxis> 
 
            <chart:LineSeries ItemsSource="{Binding Collection}" XBindingPath="XValue" YBindingPath="YValue"/> 
        </chart:SfChart> 
 
        <chart:SfChart Margin="10" Grid.Row="1" Annotations="{Binding Annotations}" DataContext="{StaticResource view}" > 
          
            <chart:SfChart.PrimaryAxis> 
                <chart:CategoryAxis/> 
            </chart:SfChart.PrimaryAxis> 
            <chart:SfChart.SecondaryAxis> 
                <chart:NumericalAxis/> 
            </chart:SfChart.SecondaryAxis> 
 
            <chart:SplineSeries ItemsSource="{Binding Collection}" XBindingPath="XValue" YBindingPath="YValue"/> 
        </chart:SfChart> 
 
 
Also, we can overcome this problem by creating separate instance to the annotations property  and add it to the chart  as like in the below code, 
MainWindow.xaml: 
<Grid> 
        <Grid.DataContext> 
            <local:ViewModel/> 
        </Grid.DataContext> 
        
        <Grid.RowDefinitions> 
            <RowDefinition /> 
            <RowDefinition/> 
        </Grid.RowDefinitions> 
        <chart:SfChart Margin="10"  Annotations="{Binding Annotations}"  > 
            <chart:SfChart.PrimaryAxis> 
                <chart:CategoryAxis/> 
            </chart:SfChart.PrimaryAxis> 
            <chart:SfChart.SecondaryAxis> 
                <chart:NumericalAxis/> 
            </chart:SfChart.SecondaryAxis> 
 
            <chart:LineSeries ItemsSource="{Binding Collection}" XBindingPath="XValue" YBindingPath="YValue"/> 
        </chart:SfChart> 
 
        <chart:SfChart Margin="10" Grid.Row="1" Annotations="{Binding Annotations1}" > 
            <chart:SfChart.PrimaryAxis> 
                <chart:CategoryAxis/> 
            </chart:SfChart.PrimaryAxis> 
            <chart:SfChart.SecondaryAxis> 
                <chart:NumericalAxis/> 
            </chart:SfChart.SecondaryAxis> 
 
            <chart:SplineSeries ItemsSource="{Binding Collection}" XBindingPath="XValue" YBindingPath="YValue"/> 
        </chart:SfChart> 
 
 
We have prepared a sample based on this and it can be downloaded from the below link, 
Regards, 
Durgadevi S 



Matthew Hewitt
Replied On September 26, 2017 08:25 AM UTC

I tried this but I am unable to set the key of the resource.


<AriaControls:ChartProxy x:Key="chartProxy" ZoomMode="{Binding chartZoomMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <AriaControls:ChartProxy.DataContext>
                                   <AriaVM:CMViewModel <Setting the key  here  causes an error that  you  can do this outside an IDictionary></AriaVM:CMViewModel>
                </AriaControls:ChartProxy.DataContext>
            </AriaControls:ChartProxy>

And then trying to use the chartProxy as the staticResource cause null reference problems.

Many Thanks

Durgadevi Selvaraj [Syncfusion]
Replied On September 27, 2017 12:34 PM UTC

Hi Matthew, 

Thanks for your update. 

We would like to inform you that, we can set key only inside “Resources” of the element . In order to access resource key inside any controls(like chart),  we need to set it to the resources of the panel(like grid) as shown in reference code.  

<Grid>   
     <Grid.Resources> 
         <local:ViewModel x:Key="view" x:Shared="false"/> 
    </Grid.Resources> 
         
        <chart:SfChart Annotations="{Binding Annotations}" DataContext="{StaticResource view}"  >          
            <chart:SfChart.PrimaryAxis> 
                <chart:CategoryAxis/> 
            </chart:SfChart.PrimaryAxis> 
            <chart:SfChart.SecondaryAxis> 
                <chart:NumericalAxis Minimum="20.5" Maximum="51" EdgeLabelsVisibilityMode="Visible"/> 
            </chart:SfChart.SecondaryAxis> 
 
            <chart:LineSeries ItemsSource="{Binding Collection}" XBindingPath="XValue" YBindingPath="YValue"/> 
        </chart:SfChart> 
</Grid> 

If you are still able to reproduced the problem in the above solution then please provide us with simple sample in order to reproduce the issue. So that, it would be more helpful for us to provide you a solution as sooner. 

Regards, 
Durgadevi S 


Matthew Hewitt
Replied On October 2, 2017 11:01 AM UTC

Hi,


We are still having a problem with this. It is on a usercontrol and as soon as we set the name of the datacontext and then set it to static we get the error. The resource cannot be resolved.


       

   

And then when trying to use it



What are we doing wrong here.
Many Thanks

EDIT----------------------
Unforunatley we are not allowed to provide any functioning code samples.

Durgadevi Selvaraj [Syncfusion]
Replied On October 3, 2017 02:16 PM UTC

Hi Matthew, 
We are unable see any reference codes or attachments about the exact problem from your previous update. Could you please share us with more details about the exact problem from your side? If possible, please provide us with simple sample to reproduce the issue, it would be helpful for us to assist you further. 
Also, as we have informed earlier, we can resolve the problem by creating separate instance to the annotation property and add it to the chart as like in the below code, 

XAML: 
<Grid>  
        <Grid.DataContext>  
            <local:ViewModel/>  
        </Grid.DataContext>  
         
        <Grid.RowDefinitions>  
            <RowDefinition />  
            <RowDefinition/>  
        </Grid.RowDefinitions>  
        <chart:SfChart Margin="10"  Annotations="{Binding Annotations}"  >  
            <chart:SfChart.PrimaryAxis>  
                <chart:CategoryAxis/>  
            </chart:SfChart.PrimaryAxis>  
            <chart:SfChart.SecondaryAxis>  
                <chart:NumericalAxis/>  
            </chart:SfChart.SecondaryAxis>  
  
            <chart:LineSeries ItemsSource="{Binding Collection}" XBindingPath="XValue"YBindingPath="YValue"/>  
        </chart:SfChart>  
  
        <chart:SfChart Margin="10" Grid.Row="1" Annotations="{Binding Annotations1}" >  
            <chart:SfChart.PrimaryAxis>  
                <chart:CategoryAxis/>  
            </chart:SfChart.PrimaryAxis>  
            <chart:SfChart.SecondaryAxis>  
                <chart:NumericalAxis/>  
            </chart:SfChart.SecondaryAxis>  
  
            <chart:SplineSeries ItemsSource="{Binding Collection}"XBindingPath="XValue" YBindingPath="YValue"/>  
        </chart:SfChart>  
  


Please find the reference sample from below link, 

Regards,  
Durgadevi S 


Matthew Hewitt
Replied On October 3, 2017 03:54 PM UTC

Hi,

We will never be allowed to share code snippets but as for your suggestion to create multiple instances of the object we tried this with having 8 of them and adding a clone of each of the VerticalLineAnnotations to all of the instances and we get the same issue with the logical children problem. How do I go about cloning the AnnotationCollection in a way that it contains 8 instances of the same set of data.

Many Thanks


Durgadevi Selvaraj [Syncfusion]
Replied On October 4, 2017 01:26 PM UTC

Hi Matthew, 

We have tried to reproduced the reported problem (adding a clone of each VerticalLineAnnotation to all the instance thrown same logical parent problem) and unable to reproduced on our end. 

We have prepared a sample based on this and please find it from the below,
Sample:Clone_AnnotationObject
 

If you are still able to reproduced the same problem then please modify the provided sample to reproduce the issue, it would be helpful for us to serve you better. 

Regards,
Durgadevi S


 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;