c# - 如何从 PropertyGridView 中删除标准属性

标签 c# .net winforms propertygrid

我正在创建一个图形界面,并希望为用户提供编辑图形外观的选项,即系列颜色、背景颜色、数据点大小等...正在使用

创建图表
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 与图表 1 连接,因此当网格中的属性发生更改时,图表 1 就会受到影响。感谢您持续的帮助。

最佳答案

您可以修改使用属性显示的对象,而不是修改 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.
}

关于c# - 如何从 PropertyGridView 中删除标准属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075712/

相关文章:

.net - 为什么 ActualSize 没有在 WPF 上更新其值?

c# - .Net 缓存过期

c# - 正在运行任务的关闭表单

c# - 如何使用半透明选择器选择屏幕上任意位置的颜色?

c# - 如何将 DataGridView 文本框列设置为多行?

c# - 将现有密码短语更改为不明显的难以猜测的短语

c# - Azure 云服务

c# - 在列表框中删除超过 1 个项目

.net - 强名称、 list 和代码签名

c# - 如何将按钮添加到 DataGridView 中的列