2

I am creating a graphing interface and would like to give the user the option to edit the graphs look, i.e. Series color, BackColor, Data point size, etc... The chart is being created using

System.Windows.Forms.DataVisualization.Charting

To allow the user to edit those options I've placed a PropertyGrid in the form. However there are some properties I do not want the user to have access to. I would like to be able to set up a chart in my form then create a propertygrid that is connected to that chart but have certain properties removed from the grid. What I have so far tried is ...

public partial class Form1: Form 
{
    PropertyGrid propG1 = new PropertyGrid();
    this.Controls.Add(propG1);

    //... There is code here where my chart(chart1) is being populated with data 
    private void toolStripButton1_Click(object sender, EventArgs e)// The button is just to test 
    MyChart myC = new MyChart(); 
    propG1.SelectedObject = myC; 
}

So based on the recomendations I've received so far I've created a class called MyChart which contains the properties that I don't want to be displayed on my chart.

using System.ComponentModel
//...

public class MyChart : Chart 
{
    [Browsable(false)]
    public new System.Drawing.Color Property
    {
        get{return BackColor;}  // BackColor is just an example not necessarily a property I'd like to remove
        set{base.BackColor = value;}
    }

I haven't been able to remove the properties from my grid nor have I been able to connect myC with my chart1 so when a property is changed in the grid chart1 is affected. Thanks for the continuing help.

4

1 回答 1

2

Instead of modifying the PropertyGrid component and its behavior you can modify the objects that you display with attributes. Something like this:

[Browsable(false)]
public object SomeProperty
{
}

Don't forget to add:

using System.ComponentModel;

And to override inherited properties and hide them from the propertyGrid you could do something like:

public class Chart : BaseChart
{
    [Browsable(false)]
    public new string BaseString // notice the new keyword!
    {
        get { return base.BaseString; } // notice the base keyword!
        set { base.BaseString = value; }
    }

    // etc.
}

public class BaseChart
{
    public string BaseString { get; set; }
}

Setting the Browsable attribute to false will keep SomeProperty from showing up in the PropertyGrid.

So, in a hypothetical chart class like the one below, you will see the chart instance, the SomeProperty1 property but not SomeProperty2.

public class Chart
{
    public object Property1 { get; set; }

    [Browsable(false)]
    public object Property2 { get; set; }

    // etc.
}

See Getting the most out of your property grid for more information. And here is a very, very good deep-dive into customizing the PropertyGrid control that will blow your mind. ;-)

And, even more fun with attributes and the PropertyGrid:

[DefaultPropertyAttribute("Property1")]
public class Chart
{
    [CategoryAttribute("My Properties"),
     DescriptionAttribute("My demo property int"),
     DefaultValueAttribute(10)]
    public int Property1 { get; set; }

    [Browsable(false)]
    public object Property2 { get; set; }

    [CategoryAttribute("My Properties"),
     DescriptionAttribute("My demo property string"),
     DefaultValueAttribute("Hello World!")]
    public string Property3 { get; set; }

    // etc.
}
于 2013-02-25T20:21:36.240 回答