How to get a notification when the legend items are clicked in WPF Chart?
This knowledge base article explains how to receive notifications when a legend item is clicked in a WPF Chart. You can achieve this through one of the following solutions:
Solution 1: Using the MouseDown Event
You can get notification when a LegendItem is clicked by utilizing the MouseDown event of ChartLegend. Additionally, you can obtain information about the corresponding LegendItem from the MouseButtonEventArgs. The following code snippet demonstrates how to receive a 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:Using MVVM Pattern
You can receive notifications in the view model using the MVVM pattern by binding a Boolean property from the view model to the associated series IsSeriesVisible property. This way, when a series legend is clicked, the IsSeriesVisible property of view model will be updated.
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)); } }
Conclusion
I hope you enjoyed learning how to get a notification when the legend items are clicked in WPF Chart.
You can refer to our WPF Chart feature tour page know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart Examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!