我正在创建一个图形界面,并希望为用户提供编辑图形外观的选项,即系列颜色,背景颜色,数据点大小等...图表正在使用
创建System.Windows.Forms.DataVisualization.Charting
为了允许用户编辑这些选项,我在表单中放置了一个PropertyGrid。但是,有些属性我不希望用户有权访问。我希望能够在我的表单中设置一个图表,然后创建一个连接到该图表的属性网格,但是从网格中删除了某些属性。 我到目前为止所尝试的是......
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;
}
因此,基于我迄今收到的建议,我创建了一个名为MyChart的类,其中包含我不希望在我的图表上显示的属性。
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;}
}
我无法从网格中删除属性,也无法将myC与我的chart1连接,因此当网格中的属性发生变化时,图表1会受到影响。感谢您的持续帮助。
答案 0 :(得分:2)
您可以修改使用属性显示的对象,而不是修改PropertyGrid组件及其行为。像这样:
[Browsable(false)]
public object SomeProperty
{
}
不要忘记添加:
using System.ComponentModel;
要覆盖继承的属性并将其隐藏在propertyGrid中,您可以执行以下操作:
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; }
}
将Browsable属性设置为false将使SomeProperty不会出现在PropertyGrid中。
因此,在如下图所示的假设图表类中,您将看到图表实例,SomeProperty1属性,但不会看到SomeProperty2。
public class Chart
{
public object Property1 { get; set; }
[Browsable(false)]
public object Property2 { get; set; }
// etc.
}
有关详细信息,请参阅Getting the most out of your property grid。这是一个非常非常好的深入研究customizing the PropertyGrid control,这会让你大吃一惊。 ; - )
而且,使用属性和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.
}