Search code examples
c#wpflivecharts

Using c# / WPF / livecharts. How can I set visibility of individual item at SeriesCollection?


I'm using SeriesCollection for my chart series.

What I want to do is toggle visibility when user check/uncheck at series list.

At XAML, ItemsControl code is like this.

<ItemsControl ItemsSource="{Binding Path=SeriesItemList}"
              ItemTemplate="{StaticResource toggleChartItemTemplate}"
                >

At item template, checkbox IsChecked event is bound like this.

        <CheckBox IsChecked="{Binding Path=IsChecked}" >
        </CheckBox>

And when IsChecked is called, it is written at class file as

    public bool IsChecked
    {
        get { return _IsChecked; }
        set
        {
            _IsChecked = value;
            Console.Write("");
            mainViewModel.ToggleSeries(SeriesName, value);
            RaisePropertyChanged("IsChecked");
        }
    }

And at last, ToggleSeries function called

    public void ToggleSeries(string SeriesName, bool value)
    {
        for (int idx = 0; idx < MainChartSeries.Count; idx++)
        {
            if (MainChartSeries[idx].Title == SeriesName)
            {
                //We will set visibility of the series here.
            }
        }
        Console.Write("");
    }

The codes work without error. So I think there is no probelm. If I can Find out How can I set visibility of the individual series visibility of SeriesCollection.

What can I access is IsSeriesVisible property. it doesn't have set method.

How can I set it's visibility? Should I change SeriesCollection to something else?

Thank you.


Solution

  • Just had the same Problem and found a solution.

    you can cast your SeriesCollection[idx] to the kind of Series you are using like so:

    LineSeries seriesToHide = (MainChartSeries[idx] as LineSeries);
    seriesToHide.Visibility = Visibility.Collapsed;
    

    And there you have your Visibility property with a setter, and even more other usefull properties.