Binding Visibility on LineSeries

4 posts, 1 answers
  1. Tyler
    Tyler avatar
    2 posts
    Member since:
    Nov 2009

    Posted 24 Jan 2012 Link to this post

    I am using a converter to bind the visibility of a LineSeries to a bool that is updated by a checkbox outside the ChartView.  Unfortunately, doing so throws NullReferenceExceptions, source code is below:

    <telerik:RadCartesianChart Grid.Row="1">
                <chartview:LineSeries ItemsSource="{Binding PropertyValues.BPOs}"
                                      ValueBinding="BPO"
                                      CategoryBinding="Dt"
                                      Visibility="{Binding BPOsVisible, Converter={StaticResource BoolToVisibilityConverter}}">
                   </chartview:LineSeries>
                <telerik:RadCartesianChart.HorizontalAxis>
                    <chartview:DateTimeContinuousAxis LabelFitMode="MultiLine"
                                                      LabelFormat="MMM yyyy"/>
                </telerik:RadCartesianChart.HorizontalAxis>
                <telerik:RadCartesianChart.VerticalAxis>
                    <chartview:LinearAxis />
                </telerik:RadCartesianChart.VerticalAxis>
    </telerik:RadCartesianChart>

    public class PropertyValues
        {
            public List<BPOValue> BPOs { get; set; }
        }

        public class BPOValue
        {
            public DateTime Dt { get; set; }
            public decimal? BPO { get; set; }
            public string BPOType { get; set; }
        }

    public class BoolToVisibilityConverter : IValueConverter
        {
            #region IValueConverter Members
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                bool? b = value as bool?;
                if (b.HasValue)
                {
                    if (b.Value)
                        return Visibility.Visible;
                    else
                        return Visibility.Collapsed;
                }
                else
                    return Visibility.Visible;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
            #endregion
        }

    The NullReferenceException is being thrown after the Converter is passing back it's value and my graph renders fine when the visibility binding is removed.  Does the Visibility property not work as I am expecting (binding a Visibility object will change the state of the line), or am I using the property wrong?  Any help would be greatly appreciated!

    Thanks!
  2. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 27 Jan 2012 Link to this post

    Hi Tyler,

    Please, find attached a small example, based on your code, which shows a line series with visibility bound to a checkbox.

    Best regards,
    Ves
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Answer
    Luke
    Luke avatar
    1 posts
    Member since:
    Mar 2011

    Posted 29 Jan 2012 Link to this post

    Hi Ves,

    I've noticed a very similar issue before.  If you take your solution, change the private field _BPOsVisible so that it is false to start with and then run it, you will get a NullReferenceException thrown from within RadChartBase.ArrangeOverride.

    The problem is not the binding on the Visibility.  It seems the problem is that RadCartesianChart doesn't like being initialised with a collapsed series.  In fact, the following XAML is sufficient to reproduce the problem:

            <telerik:RadCartesianChart xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
               <telerik:LineSeries Visibility="Collapsed" />
            </telerik:RadCartesianChart>

    The workaround for Tyler is to set up the Visibility binding in code-behind.  If the RadCartesianChart is within a UserControl, a suitable place to set up the binding is in an event handler for the UserControl's Loaded event.

    I apologise for not filing a bug report about this.  My account has been added to a support contract which has expired, but despite me chasing my client several times, it still hasn't been renewed.
  5. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 02 Feb 2012 Link to this post

    Hi Luke,

    Thanks for the heads up. I have logged the problem here. Hope setting it in code behind is applicable.

    Best regards,
    Ves
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top