How to get a notification when the legend items are clicked in WPF Chart?
This KB explains How to get notification when the legend item is clicked in WPF Chart
You can notify the legend toggling by following anyone of the solutions.
Solution 1:
You can get notification when a LegendItem is clicked using the MouseDown event of ChartLegend and get information of the corresponding LegendItem from the MouseButtonEventArgs. The following code snippet demonstrates how to get notification for ChartLegend.
Xaml:
<chart:SfChart x:Name="chart" > … <chart:SfChart.Legend> <chart:ChartLegend ToggleSeriesVisibility="True" MouseDown="ChartLegend_MouseDown"/> </chart:SfChart.Legend> … </chart:SfChart>
C#:
private void ChartLegend_MouseDown(object sender, MouseButtonEventArgs e) { var element = e.OriginalSource as FrameworkElement; var legendItem = element.DataContext as LegendItem; if (legendItem != null) { var series = legendItem.Series; } }
Solution 2:
You can get notification in view model through the MVVM pattern by binding the Boolean property (the property that holds the state of the series visibility) from view model to the associated series IsSeriesVisible property. In this way, when the series legend is clicked, IsSeriesVisible property of view model will be notified.
Xaml:
<syncfusion:LineSeries IsSeriesVisible="{Binding IsSeriesVisible, Mode=TwoWay}" ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue" Label="LineSeries" />
C#:
public class ViewModel : INotifyPropertyChanged { private bool isSeriesVisible = true; public event PropertyChangedEventHandler PropertyChanged; public bool IsSeriesVisible { get { return isSeriesVisible; } set { isSeriesVisible = value; OnPropertyChanged(); } } private void OnPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }