PieChart SelectedPoints binding exception

2 posts, 0 answers
  1. Nikolay
    Nikolay avatar
    1 posts
    Member since:
    May 2013

    Posted 10 Mar 2015 Link to this post

    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?
  2. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 12 Mar 2015 Link to this post

    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.

     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top