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

PieChart SelectedPoints binding exception

1 Answer 131 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Nikolay
Top achievements
Rank 1
Nikolay asked on 10 Mar 2015, 05:49 AM
Hello,

I'm using PieChart with VM-binding and have ArgumentNullException exception in ChartSelectionBehavior.Branch.cs:
internal void UpdateSelectedPointsAndRaiseSelectionChanged()
        {
            var oldSelectedPoints = this.chart.SelectedPoints; // SelectedPoints is always null
            var newSelectedPoints = this.GetSelectedPoints();
 
            ////var oldSelectedSeries = this.chart.SelectedSeries;
            ////var newSelectedSeries = this.GetSelectedSeries();
 
            var addedPoints = newSelectedPoints.Except(oldSelectedPoints).ToList(); //exception here
            var removedPoints = oldSelectedPoints.Except(newSelectedPoints).ToList();

My XAML:
<telerik:RadPieChart SelectedPoints="{Binding SelectedPoints}">
    <telerik:RadPieChart.Resources>
        <Style x:Key="PieSliceStyle" TargetType="Path">
            <Setter Property="Fill" Value="{Binding DataItem.Brush}" />
        </Style>
    </telerik:RadPieChart.Resources>
    <telerik:RadPieChart.SmartLabelsStrategy>
        <telerik:PieChartSmartLabelsStrategy DisplayMode="SpiderAlignedOutwards"/>
    </telerik:RadPieChart.SmartLabelsStrategy>
    <telerik:RadPieChart.Behaviors>
        <telerik:ChartSelectionBehavior DataPointSelectionMode="Multiple"
                                        x:Name="PieSelectionBehavior"/>
    </telerik:RadPieChart.Behaviors>
    <telerik:RadPieChart.Series>
        <telerik:DoughnutSeries ItemsSource="{Binding PieChartErrors}"
                                ValueBinding="Count"
                                DefaultSliceStyle="{StaticResource PieSliceStyle}">
            <telerik:DoughnutSeries.LegendSettings>
                <telerik:DataPointLegendSettings TitleBinding="Description"/>
            </telerik:DoughnutSeries.LegendSettings>
            <telerik:DoughnutSeries.LabelConnectorsSettings>
                <telerik:ChartSeriesLabelConnectorsSettings/>
            </telerik:DoughnutSeries.LabelConnectorsSettings>
        </telerik:DoughnutSeries>
    </telerik:RadPieChart.Series>
</telerik:RadPieChart>

ViewModel:
public class ErrorsAndWarningsViewModel : Page
{
    private IObservableCollection<PieChartError> _pieChartErrors;
    private ReadOnlyDataPointCollection _selectedPoints;
 
    public ErrorsAndWarningsViewModel
    {
        PieChartErrors = new BindableCollection<PieChartError>();
        SelectedPoints = new ReadOnlyDataPointCollection();
    }
 
    public IObservableCollection<PieChartError> PieChartErrors
    {
        get { return _pieChartErrors; }
        set
        {
            if (Equals(value, _pieChartErrors))
            {
                return;
            }
            _pieChartErrors = value;
            NotifyOfPropertyChange();
        }
    }
 
    public ReadOnlyDataPointCollection SelectedPoints
    {
        get { return _selectedPoints; }
        set
        {
            _selectedPoints = value;
            NotifyOfPropertyChange();
        }
    }
 
    public class PieChartError
    {
        public string Description { get; set; }
        public int Count { get; set; }
        public Brush Brush { get; set; }
    }
}

What am I doing wrong?

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 12 Mar 2015, 02:42 PM
Hi Nikolay,

The reported exception is thrown because the binding of the SelectedPoints collection kicks in before the setting of the UserControl's DataContext. Therefore, the SelectedPoints collection is not yet created and its value is null. To resolve this you can set the DataContext of the user control before its InitializeComponent() method. Here is an example:
public MainWindow()
{
    this.DataContext = new ErrorsAndWarningsViewModel();
    InitializeComponent(); 
}
Please try this and let me know if it helps.

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ChartView
Asked by
Nikolay
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or