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. Image for the cookie policy date

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

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


7 Replies

DS Durgadevi Selvaraj Syncfusion Team 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 




MH Matthew Hewitt 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


DS Durgadevi Selvaraj Syncfusion Team 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 



MH Matthew Hewitt 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.


DS Durgadevi Selvaraj Syncfusion Team 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 



MH Matthew Hewitt 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



DS Durgadevi Selvaraj Syncfusion Team 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


 


Loader.
Live Chat Icon For mobile
Up arrow icon