如何从PropertyGridView中删除标准属性
本文关键字:删除 标准 属性 PropertyGridView | 更新日期: 2023-09-27 18:00:57
我正在创建一个图形界面,并希望为用户提供编辑图形外观的选项,即系列颜色、背景颜色、数据点大小等。图表正在使用创建
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连接,因此当网格中的属性更改时,chart1会受到影响。感谢您的持续帮助。
您可以修改使用属性显示的对象,而不是修改PropertyGrid组件及其行为。类似这样的东西:
[Browsable(false)]
public object SomeProperty
{
}
别忘了添加:
using System.ComponentModel;
要覆盖继承的属性并从属性网格中隐藏它们,您可以执行以下操作:
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; }
}
将Browseable属性设置为false将阻止SomeProperty显示在PropertyGrid中。
因此,在下面这样一个假设的图表类中,您将看到图表实例,即SomeProperty1属性,而不是SomeProperty2。
public class Chart
{
public object Property1 { get; set; }
[Browsable(false)]
public object Property2 { get; set; }
// etc.
}
有关详细信息,请参阅充分利用属性网格。这里有一个非常非常好的关于自定义PropertyGrid控件的深入研究,这会让你大吃一惊
而且,属性和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.
}