Articles in this section
Category / Section

How to change the series selection when clicking the legend items in WPF Chart?

1 min read

Description

This article demonstrates how to change the the Interior of a Series when its LegendItem is clicked. By handling the MouseDown event of the ChartLegend and you can get the Series of corresponding LegendItem from the MouseButtonEventArgs.

 

Solution

 

To achieve this functionality, follow the steps below:

 

Step 1: Define the ChartLegend with MouseDown Event.

 

XAML:

 
   <chart:SfChart.Legend>
         <chart:ChartLegend MouseDown="ChartLegend_MouseDown"/>
   </chart:SfChart.Legend>
 

Step 2: Implement the MouseDown Event Handler.

 

In the code-behind, implement the ChartLegend_MouseDown event handler to toggle the Interior property of the clicked series.

 

C#:

  
    public partial class MainWindow : Window
    {
        private ChartSeriesBase prevSeries;
        private void ChartLegend_MouseDown(object sender, MouseButtonEventArgs e)
        {       …
                if (series != null)
                {
                    if (prevSeries != series && prevSeries != null)
                        prevSeries.Interior = null;
 
                    if (series.Interior == null)
                        series.Interior = new SolidColorBrush(Colors.Red);
                    else
                        series.Interior = null;
 
                    prevSeries = series;
                }
        }
    }
 

In this example, clicking a legend item toggles the interior color of the corresponding series indicating selection. 


Conclusion

I hope you enjoyed learning about how to change the series selection when clicking the legend items in WPF Chart.

You can refer to our WPF Chart feature tour page to 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 example 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied