How do I remove the legend from a Silverlight 4 PieSeries Pie Chart

I have created a Chart using a PieSeries in Silverlight 4 via c# code.  I need to remove the legend (via code).  Nothing I have tried has worked, any suggestions?
chester427Asked:
Who is Participating?
 
politexCommented:
It's working sample, Legent class is in the System.Windows.Controls.DataVisualization namespace. Let me know if you have any questions
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;


namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        public List<MyItem> data = new List<MyItem>();
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Chart myChart = new Chart();            
            PieSeries pe = new PieSeries();
            myChart.Series.Add(pe);
            data.Add(new MyItem { Name = "item1", Val = 3 });
            data.Add(new MyItem { Name = "item2", Val = 5 });
            data.Add(new MyItem { Name = "item3", Val = 4 });
            
            pe.DependentValueBinding = new Binding("Val");
            pe.ItemsSource= data;
            myChart.LegendStyle= new Style(typeof(Legend));
            myChart.LegendStyle.Setters.Add(new Setter(HeightProperty, 0));
            myChart.LegendStyle.Setters.Add(new Setter(WidthProperty, 0));
            LayoutRoot.Children.Add(myChart);
        }
      
    }
    public class MyItem
    {
        public string Name { get; set; }
        public int Val { get; set; }
    }
}

Open in new window

0
 
politexCommented:
Hi, it's dirty hack, but works:
myChart.LegendStyle.Setters.Add(new Setter(HeightProperty, 0));
myChart.LegendStyle.Setters.Add(new Setter(WidthProperty, 0));

Open in new window

0
 
chester427Author Commented:
Thanks for responding Politex, but I get a null reference exception.  I had tried something similar with the PieSeries that is attached to the chart by creating a stye and setting the LegendItem.HeightProperty and LegenItem.WidthProperty to 0 (with a setter) and then assigning the style object to the PieSeries.  This worked fine, but only removed(hid) the items from the legend - it still left an empty legend - basically a gray box.  Any suggestions?
0
 
chester427Author Commented:
Thanks for posting the example.  I was just missing a reference.  After adding it -it all fell into place.

Super!
0
 
chester427Author Commented:
I appreciated the help I received.  Politex, thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.