This is a migrated thread and some comments may be shown as answers.

Issue using SeriesLegendSettings with ChartSeriesProvider

8 Answers 364 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 14 Aug 2013, 04:21 PM
I'm attempting to add a basic legend to go along with my dynamically generated line series:
<telerik:RadCartesianChart.SeriesProvider>
   <telerik:ChartSeriesProvider Source="{Binding VisiblePortfolios}">
      <telerik:ChartSeriesProvider.SeriesDescriptors>
         <telerik:CategoricalSeriesDescriptor ItemsSourcePath="EquityCurve" ValuePath="Value" CategoryPath="Date">
            <telerik:CategoricalSeriesDescriptor.Style>
               <Style TargetType="telerik:LineSeries">
                  <Setter Property="StrokeThickness" Value="2"/>
                  <Setter Property="LegendSettings">
                     <Setter.Value>
                        <telerik:SeriesLegendSettings Title="{Binding Name}"/>
                     </Setter.Value>
                  </Setter>
               </Style>
            </telerik:CategoricalSeriesDescriptor.Style>
         </telerik:CategoricalSeriesDescriptor>
      </telerik:ChartSeriesProvider.SeriesDescriptors>
   </telerik:ChartSeriesProvider>
</telerik:RadCartesianChart.SeriesProvider>

However, this error occurs when I debug:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.Name; DataItem=null; target element is 'SeriesLegendSettings' (HashCode=10926182); target property is 'Title' (type 'String')

And the legend does not appear at all. Any assistance is greatly appreciated.

8 Answers, 1 is accepted

Sort by
0
Accepted
Ves
Telerik team
answered on 19 Aug 2013, 01:32 PM
Hi Michael,

Please, find attached a small example, which shows the described scenario in action with the legend appearing as expected. Please, make sure that you use the latest binaries.

Best regards,
Ves
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Michael
Top achievements
Rank 1
answered on 21 Aug 2013, 04:18 PM
I greatly appreciate the assistance, thank you very much for your help in solving this problem.
0
Philip
Top achievements
Rank 1
answered on 14 May 2015, 12:59 PM

Hi, I tried the attached SeriesProviderWithLegend.

When I run the example, the chart shows "No data to plot". However the legends displays Name1, Name2 and Name3 as expected.

Any idea what could be wrong?

I tried against UI for WPF Q1 2015\Binaries.NoXaml\WPF40 and Visual Studio 2013 Themes

0
Philip
Top achievements
Rank 1
answered on 15 May 2015, 06:36 AM
I also get the following message in the output window :
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Name; DataItem=null; target element is 'SeriesLegendSettings' (HashCode=59586475); target property is 'Title' (type 'String')

(I wanted to edit previous post, but seems it is not possible for a normal user. Admin, feel free to edit)
0
Martin Ivanov
Telerik team
answered on 15 May 2015, 04:28 PM
Hi Philip,

Note that when you create a custom style for any of our controls and use the NoXaml assemblies you will need to based this style to the default one. Otherwise, the default style will be overridden completely, including the template of the control, which will be empty. This is the reason why the LineSeries are not displayed. In order to resolve this you will need to set the BasedOn property with the appropriate key on the series descriptor's style. Here is an example:
<telerik:CategoricalSeriesDescriptor.Style>
    <Style TargetType="telerik:LineSeries" BasedOn="{StaticResource LineSeriesStyle}">
        <Setter Property="StrokeThickness" Value="4"/>
        <Setter Property="Stroke" Value="Red"/>
        <Setter Property="CombineMode" Value="Stack"/>
        <Setter Property="LegendSettings">
            <Setter.Value>
                <telerik:SeriesLegendSettings Title="{Binding Name, TargetNullValue=asdf, FallbackValue=kle}"/>
            </Setter.Value>
        </Setter>
    </Style>
</telerik:CategoricalSeriesDescriptor.Style>

About the error, it appears only in the output window of Visual Studio. The error will be thrown only if the CLR exception of VS are enabled. Also, the exception doesn't reflect on the application's proper work - everything works as expected. It seems that, the binding of the legend setting's Title kicks in before the DataItem is assigned and the binding is reflected a little bit later which leads to the error at the first place. However, after the DataItem is set the Title's binding getting the correct value and the legend item is generated properly. 

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Philip
Top achievements
Rank 1
answered on 19 May 2015, 06:41 AM
Thanks, that solved the problem. This kind of binding to a dynamic list of graphs is very useful :)
0
Johannes
Top achievements
Rank 1
answered on 15 Feb 2017, 09:29 AM

We created our own control. This controls shall include a telerik chart. In my stand alone solution, I could make it work, but now I'm searching for hours and can't figure it out. I get the same error message and I can see no Legend.

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Name; DataItem=null; target element is 'SeriesLegendSettings' (HashCode=44086926); target property is 'Title' (type 'String')

We are using the NoXAML dlls and I included the xaml file in my merged dictionary:

<wpfu:SharedResourceDictionary  Source="pack://application:,,,/[...]/Telerik/Telerik.Windows.Controls.Chart.xaml"/>

Here is the Docking Panel that we use: (I needed to strip anything that had nothing to do with the chart)

<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Border DockPanel.Dock="Right"
            VerticalAlignment="Center"
            BorderThickness="0"
            Visibility="{Binding Path=ShowLegend, Converter={StaticResource VisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}"
            Background="{Binding Background}"
            HorizontalAlignment="Stretch">
        <telerik:RadLegend Items="{Binding Path=LegendItems, ElementName=ChartObject}" Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" Margin="5"/>
    </Border>
    <telerik:RadCartesianChart x:Name="ChartObject"
                               Palette="{Binding Path=Palette, RelativeSource={RelativeSource TemplatedParent}}"
                               DockPanel.Dock="Left">
        <telerik:RadCartesianChart.Resources>
            <Style TargetType="telerik:BarSeries" BasedOn="{StaticResource BarSeriesStyle}">
                <!--Setter Property="CombineMode" Value="Stack"/-->
                <Setter Property="ShowLabels" Value="True"/>
                <Setter Property="LegendSettings">
                    <Setter.Value>
                        <telerik:SeriesLegendSettings Title="{Binding Path=Name}"/>
                    </Setter.Value>
                </Setter>
            </Style>
 
 
            <Style TargetType="telerikChartView:LineSeries" BasedOn="{StaticResource LineSeriesStyle}">
                <Setter Property="StrokeThickness" Value="12"/>
                <Setter Property="LegendSettings">
                    <Setter.Value>
                        <telerik:SeriesLegendSettings Title="{Binding Path=Name}"/>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style TargetType="telerikChartView:AreaSeries" BasedOn="{StaticResource AreaSeriesStyle}">
                <Setter Property="CombineMode" Value="Stack"/>
                <Setter Property="LegendSettings">
                    <Setter.Value>
                        <telerik:SeriesLegendSettings Title="{Binding Path=Name}"/>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style TargetType="telerik:PointSeries" BasedOn="{StaticResource PointSeriesStyle}">
                <Setter Property="ShowLabels" Value="True"/>
                <Setter Property="LegendSettings">
                    <Setter.Value>
                        <telerik:SeriesLegendSettings Title="{Binding Path=Name}"/>
                    </Setter.Value>
                </Setter>
            </Style>
 
        </telerik:RadCartesianChart.Resources>
        <telerik:RadCartesianChart.SeriesProvider>
            <telerik:ChartSeriesProvider Source="{Binding Path=ChartDataSeries, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" >
                <telerik:ChartSeriesProvider.SeriesDescriptors>
                    <telerik:CategoricalSeriesDescriptor CategoryPath="X" ValuePath="Y" TypePath="Type" ItemsSourcePath="Values" />
                </telerik:ChartSeriesProvider.SeriesDescriptors>
            </telerik:ChartSeriesProvider>
        </telerik:RadCartesianChart.SeriesProvider>
        <telerik:RadCartesianChart.HorizontalAxis>
            <telerik:DateTimeCategoricalAxis LabelFormat="{Binding Path=DateFormat, RelativeSource={RelativeSource TemplatedParent}}"
                                             Title="{Binding Path=AxisLabelX, RelativeSource={RelativeSource TemplatedParent}}"
                                             Visibility="{Binding Path=ShowXAxis, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource VisibilityConverter}}"
                                             Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
                                             LabelFitMode="Multiline"
                                             ElementBrush="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
                                             SmartLabelsMode="SmartStep"/>
        </telerik:RadCartesianChart.HorizontalAxis>
        <telerik:RadCartesianChart.VerticalAxis>
            <telerik:LinearAxis LabelFormat="{Binding Path=YAxisLabelFormat, RelativeSource={RelativeSource TemplatedParent}}"
                                Title="{Binding Path=AxisLabelY, RelativeSource={RelativeSource TemplatedParent}}"
                                Visibility="{Binding Path=ShowYAxis, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource VisibilityConverter}}"
                                Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
                                LabelFitMode="Multiline"
                                ElementBrush="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
                                SmartLabelsMode="SmartStepAndRange"/>
        </telerik:RadCartesianChart.VerticalAxis>
        <telerik:RadCartesianChart.Behaviors>
            <telerik:ChartTooltipBehavior InitialShowDelay="0"
                                          BetweenShowDelay="0"
                                          Placement="Bottom"
                                          ShowDuration="5000" />
        </telerik:RadCartesianChart.Behaviors>
        <telerik:RadCartesianChart.TooltipTemplate>
            <DataTemplate>
                <Border Background="White"
                        BorderBrush="Black"
                        BorderThickness="1">
                    <StackPanel Orientation="Vertical"
                                Margin="5">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Value: " Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                            <TextBlock Text="{Binding Path=Value}" Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Time: " Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                            <TextBlock Text="{Binding Path=Category}" Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </telerik:RadCartesianChart.TooltipTemplate>
        <telerik:RadCartesianChart.PlotAreaStyle>
            <Style TargetType="Border">
                <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>
            </Style>
        </telerik:RadCartesianChart.PlotAreaStyle>
    </telerik:RadCartesianChart>
</DockPanel>
0
Martin Ivanov
Telerik team
answered on 17 Feb 2017, 02:17 PM
Hello Johannes,

Such errors could occur commonly in the WPF framework. However, they are internally handled by the framework and they don't impact on the application. It is going to run properly anyway.

What happens basically is that the binding of the Title property from the implicit style defined in the chart's Resources collection is evaluated at too early stage - before the chart's data context is loaded. This is why there is an error which is only printed in the Output window of Visual Studio, when the default exception settings are used. If you enable throwing of some additional exceptions the error could be thrown at development time. I guess this is the case on your side. If you click continue the application will run. And when the data context is loaded the bindings will be re-evaluated.

Regards,
Martin
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
ChartView
Asked by
Michael
Top achievements
Rank 1
Answers by
Ves
Telerik team
Michael
Top achievements
Rank 1
Philip
Top achievements
Rank 1
Martin Ivanov
Telerik team
Johannes
Top achievements
Rank 1
Share this question
or