We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get a notification when the legend items are clicked in WPF Chart?

Platform: WPF |
Control: SfChart

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

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile