adding custom legend creates nullreference exception

  <syncfusion:ChromelessWindow x:Class="theRightDirection.PortalAnalyzer.Desktop.Controls.Basemaps.Basemaps"
                              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                              xmlns:local="clr-namespace:theRightDirection.PortalAnalyzer.Desktop.Controls.Basemaps"
                              xmlns:syncfusion="clr-namespace:Syncfusion.Windows.Shared;assembly=Syncfusion.Shared.WPF"
                              xmlns:chart="clr-namespace:Syncfusion;assembly=Syncfusion.SfChart.WPF"
                              xmlns:charts="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF"
                              xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
                              xmlns:legend="clr-namespace:theRightDirection.PortalAnalyzer.Controls.Legend;assembly=theRightDirection.PortalAnalyzer.Controls.Legend"
                              xmlns:enums="clr-namespace:theRightDirection.PortalAnalyzer.Controls.Legend.Enumerations;assembly=theRightDirection.PortalAnalyzer.Controls.Legend"
                              xmlns:drawing="clr-namespace:System.Drawing;assembly=System.Drawing"
                              xmlns:converters="clr-namespace:theRightDirection.PortalAnalyzer.Desktop.Controls.Basemaps.Converters"
                              xmlns:converters2="clr-namespace:theRightDirection.PortalAnalyzer.Desktop.Controls.Converters;assembly=theRightDirection.PortalAnalyzer.Desktop.Controls"
                              mc:Ignorable="d"
                              TitleFontSize="18"
                              Background="White"
                              TitleTextAlignment="Center"
                              CornerRadius="0"
                              BorderBrush="White"
                              BorderThickness="11"
                              AllowsTransparency="True"
                              TitleBarBackground="#81B844"
                              ShowInTaskbar="False"
                              ResizeBorderThickness="1"
                              ResizeBorderBrush="#686A76"
                              WindowStartupLocation="CenterScreen"
                              Name="basemapwindow"
                              syncfusion:SkinStorage.VisualStyle="Default" ResizeMode="NoResize" Icon="logo.ico" ShowActivated="False"
                              Title="Basemaps information" Height="600" Width="800">
    <syncfusion:ChromelessWindow.Resources>
        <converters2:ListCountToVisibilityConverter x:Key="listCountToVisibilityConverter" />
        <converters:DeprecatedColorConverter x:Key="deprecatedColor"/>
        <converters:DeprecatedFontWeightConverter x:Key="deprecatedFontWeight"/>
        <DataTemplate x:Key="itemTemplate">
                <StackPanel Orientation="Horizontal" ToolTip="{Binding Item.Tooltip}">
                    <CheckBox IsChecked="{Binding IsSeriesVisible, Mode=TwoWay}"/>
                    <Border BorderBrush="Black" BorderThickness="1" Width="17" Height="17" Margin="0 0 2 0">
                        <Rectangle Fill="{Binding Interior}"></Rectangle>
                    </Border>
                    <TextBlock FontWeight="{Binding Item.IsDeprecated, Converter={StaticResource deprecatedFontWeight}}"  Foreground="{Binding Item.IsDeprecated, Converter={StaticResource deprecatedColor}}" Text="{Binding Label}" FontSize="10" VerticalAlignment="Center"/>
                </StackPanel>
            </DataTemplate>
    </syncfusion:ChromelessWindow.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <charts:SfChart Grid.Row="2" Margin="5" AreaBorderThickness="0">
            <charts:SfChart.Legend>
                <charts:ChartLegend ItemTemplate="{StaticResource itemTemplate}" Orientation="Vertical" DockPosition="Left" ToggleSeriesVisibility="True" CheckBoxVisibility="Visible"/>
            </charts:SfChart.Legend>
            <charts:PieSeries  XBindingPath="Label" ItemsSource="{Binding Data}" YBindingPath="Count"/>
        </charts:SfChart>
        <legend:IconLegendItem Visibility="{Binding DataContext.WebmapWithOnlyBasemapCount, Converter={StaticResource listCountToVisibilityConverter}, ConverterParameter=0, ElementName=basemapwindow}" Grid.Row="0" LegendText="3 webmaps with only a basemap" IconColor="Red" IconFamily="{x:Static enums:IconFamily.Modern}" IconName="Map" Background="#686A76"/>
        <legend:IconLegendItem Visibility="{Binding DataContext.DeprecatedBasemapsCount, Converter={StaticResource listCountToVisibilityConverter}, ConverterParameter=0, ElementName=basemapwindow}" Grid.Row="1" LegendText="90 webmaps with out of support basemap" IconColor="Red" IconFamily="{x:Static enums:IconFamily.Octicons}" IconName="Globe" Background="#686A76"/>
    </Grid>
</syncfusion:ChromelessWindow>


I bind a viewmodel to the datacontext of this window. When I remove the Legend-part the nullreference dis appear. In the stacktrace I can only see that the nullreference happens in the SetLegendItem-method.

My hypothesis is that the WPF-elements are created before the data is bound to the viewmodel. So the binding to Data is a binding to an empty list of datapoints.
What can I do to avoid this error?





5 Replies

YP Yuvaraj Palanisamy Syncfusion Team October 19, 2020 12:58 PM UTC

 
Greetings from Syncfusion. 
 
We have analyzed your query and we have prepared the sample based on your example code, the custom legend was working fine at our end. Also, we have attached the sample for your reference. 
 
Please find the sample from the below link. 
 
  
Configuration detail: 
 
Syncfusion version – 18.3.0.40 
 
Could you please revert us the following details which will be provide the better solution at the earliest. 
 
·       Please share the stack trace report for that crash. 
·       If possible, could you please revert us the modified sample with reproduce the issue. 
·       Please share your application Syncfusion nuget package version 
  
Regards, 
Yuvaraj 



MA Mannus October 19, 2020 07:29 PM UTC

Hi,

i tried already the latest nuget-version, made no difference.
stacktrace; Nullreference exception,   at Syncfusion.UI.Xaml.Charts.ChartBase.SetLegendItems(ChartLegend legend, Int32 index)

it is hard for me to share my code because of the dependencies unfortunately


YP Yuvaraj Palanisamy Syncfusion Team October 20, 2020 11:38 AM UTC

 
Thanks for your update.  
  
We have analyzed your query and suspected that it may occur since you have defined the IconLegendItem outside of chart reference, you have directly added that as an element of Grid. We checked; it seems like you have used the custom LegendItem. We would like to let you know that LegendItems are deeply coupled with the chart. You can’t define as like you did.   
  
Could you please share your use case of adding custom legend items outside of the chart?   
   
Could you please confirm in which legend part, did you face this issue either inside the chart or outside of the chart?  
   
Is it possible to share the complete stack trace?  
 
Regards, 
Yuvaraj 



MA Mannus October 20, 2020 12:56 PM UTC

Hello,

I have already included the full stacktrace. It was only:
stacktrace; Nullreference exception,   at Syncfusion.UI.Xaml.Charts.ChartBase.SetLegendItems(ChartLegend legend, Int32 index)

Besides that: legend:IconLegendItem, that is a reference to my own controls. Developed by myself and coming from another library than the SyncFusion ones.





YP Yuvaraj Palanisamy Syncfusion Team October 21, 2020 01:16 PM UTC

Hi Mannus,  
  
Thanks for your update.

Under your account, we have created a support incident to track the status of this reported query. 
 
 
Please log on to our support website to check for further updates.   

Link: https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents/
  
 
Regards,  
Yuvaraj.  


Loader.
Up arrow icon