首页 > 解决方案 > 使用 c#/WPF/livecharts。如何在 SeriesCollection 中设置单个项目的可见性?

问题描述

我正在为我的图表系列使用 SeriesCollection。

我想要做的是当用户在系列列表中选中/取消选中时切换可见性。

在 XAML 中,ItemsControl 代码是这样的。

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

在项目模板中,复选框 IsChecked 事件是这样绑定的。

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

当 IsChecked 被调用时,它被写在类文件中

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

最后,调用了 ToggleSeries 函数

    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("");
    }

代码可以正常工作。所以我认为没有问题。如果我能找出如何设置 SeriesCollection 的单个系列可见性的可见性。

我可以访问的是 IsSeriesVisible 属性。它没有设置方法。

如何设置它的可见性?我应该将 SeriesCollection 更改为其他内容吗?

谢谢你。

标签: c#wpflivecharts

解决方案


刚刚遇到同样的问题并找到了解决方案。

您可以将您的SeriesCollection[idx]转换为您正在使用的系列,如下所示:

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

在那里,您拥有带有 setter 的 Visibility 属性,以及更多其他有用的属性。


推荐阅读