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 create custom legend items in WPF Chart?

Platform: WPF |
Control: SfChart

This article describes how to define the custom legend items with view model data. 


Custom legend items are achieved by adding required LegendItem count to ChartLegend’s Items property with Item, Series, IconHeight, IconWidth, IconVisibility, Interior and ItemTemplate as per the below code snippet




<DataTemplate x:Key="itemTemplate"> 
        <StackPanel Margin="10,0,10,0"  Orientation="Horizontal"> 
                    <CheckBox Margin="2" Tag="{Binding}" 
                    <Rectangle  Width="{Binding IconWidth}"  
                                Height="{Binding IconHeight}"                                         
                                Fill="{Binding Interior}" Margin="2"/> 
                    <TextBlock HorizontalAlignment="Center" 
                               Text="{Binding Item.XValue}"> 




          ChartLegend legend = new ChartLegend(); 
            foreach (var series in chart.Series) 
                var data = series.ItemsSource as ObservableCollection<Model>; 
                for (int i = 0; i < data.Count; i++) 
                    legend.Items.Add(new LegendItem() 
                        Item = data[i].XValue.ToString(), 
                        Series = series, 
                        IconHeight = 10, 
                        IconWidth = 10, 
                        IconVisibility = Visibility.Visible, 
                        Interior = series1.ColorModel.GetBrush(i),
                        CheckBoxVisibility = Visibility.Visible
            legend.ItemTemplate = grid.Resources["itemTemplate"] as DataTemplate; 
            chart.Legend = legend; 


        private void CheckBox_Checked(object sender, RoutedEventArgs e) 
            CheckBox box = sender as CheckBox; 
            LegendItem item = box.Tag as LegendItem; 
            UpdateLegend(true, item.Item as Model); 
        private void CheckBox_Unchecked(object sender, RoutedEventArgs e) 
            CheckBox box = sender as CheckBox; 
            LegendItem item = box.Tag as LegendItem; 
            UpdateLegend(false, item.Item as Model); 
        private void UpdateLegend(bool isChecked, Model item) 
            foreach (PieSeriesExt series in chart.Series) 
                if (isChecked) 
                series.GetType().GetField("ToggledLegendIndex", BindingFlags.NonPublic | 
                    BindingFlags.Instance).SetValue(series, toggleIndexes); 
        private void UpdateArea() 
            MethodInfo info = chart.GetType().GetMethod("UpdateArea", 
                               BindingFlags.NonPublic | BindingFlags.Instance, 
                               new Type[] { typeof(bool) }, 
            info?.Invoke(chart, new object[] { true }); 


Before custom legend toggling

After custom legend togglinng  

Download complete sample here.


2X faster development

The ultimate WPF UI toolkit to boost your development speed.
You must log in to leave a comment
Brent Hoskisson
Sep 15, 2021

You have item.Item as Model. What is Model????



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