How to get a notification when the legend item is clicked in UWP Chart?
This article explains how to receive notifications when a legend item is clicked in UWP Chart.
There are two approaches to achieve this: using the PointerPressed event of ChartLegend from UIElement or by binding the IsSeriesVisible property to your ViewModel. Both methods allow you to respond to legend interactions and implement custom logic when you toggle series visibility in your charts.
Using PointerPressed Event
Step 1: In your XAML, define a ChartLegend and attach the PointerPressed event handler to capture when a legend item is clicked.
<chart:SfChart >
. . .
<chart:SfChart.Legend>
<chart:ChartLegend PointerPressed="ChartLegend_PointerPressed" />
</chart:SfChart.Legend>
<chart:SplineSeries XBindingPath="Year"
ItemsSource="{Binding List}"
YBindingPath="India"
ShowTooltip="True">
</chart:SplineSeries>
. . .
</chart:SfChart>Step 2: In the code-behind, implement the event handler to extract the information of the LegendItem that was clicked and access its associated series.
private void ChartLegend_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var element = e.OriginalSource as FrameworkElement;
LegendItem legendItem = element.DataContext as LegendItem;
if (legendItem != null)
{
var series = legendItem.Series;
}
}Using IsSeriesVisible Binding in ViewModel
Step 1: In your XAML, bind the IsSeriesVisible property of the chart series to a property in your ViewModel with TwoWay mode.
<syncfusion:LineSeries IsSeriesVisible="{Binding IsSeriesVisible, Mode=TwoWay}"
ItemsSource="{Binding Data}"
XBindingPath="XValue"
YBindingPath="YValue" />Step 2: Implement the IsSeriesVisible property in your ViewModel with INotifyPropertyChanged to receive notifications when the LegendItem is clicked.
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 about how to get notification when the legend item is clicked in UWP Chart.
You can refer to our UWP Chart’s feature tour page to know about its other groundbreaking feature representations. You can also explore our UWP Chart’s documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms 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 UWP Charts and other UWP components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!