1

我正在开发一个绘图程序,我希望用户能够快速有效地自定义他们的图表。我创建了一个允许他们这样做的属性网格。我已经用图表的属性填充了它,但删除了我不希望用户访问的某些元素。例如,我不希望用户能够访问辅助功能选项。到目前为止我所拥有的是

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        magRadioBox.Checked = true;
        PropertyGrid propertyGrid1 = new PropertyGrid();
        propertyGrid1.CommandsVisibleIfAvailable = true;
        propertyGrid1.Text = "Graph and Plotting Options";
        propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;

        this.Controls.Add(propertyGrid1);
    }
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "MY Plot Program";
propertyGrid1.SelectedObject = chart1; 
}

private void button1_Click(object sender, EventArgs e)
 {//some code that is populating my chart(chart1) with data 
 .... //chart1 being filled with data 
 }

private void propertyGrid1_PropertyValueChanged(object s , PropertyValueChangedEventArgs e)
{
//Calling the method that will refresh my chart1 
myChart.Invalidate(); 
}

上面的代码是我的表格。下面我的“MyChart”类代码设置了我的属性网格。我自动获取图表的所有属性,然后可以通过将其设置为“樱桃挑选”我不希望用户拥有的属性[Browsable(false)]

namespace FFT_Plotter
{ 
    [DefaultPropertyAttribute("Text")]
    public class MyChart : Chart 
    {
        public event EventHandler PropertyChanged;
private void OnPropertyChanged(object sender, EventArgs e)
{
EventHandler eh = propertyChanged;
if(eh !=null)
{
eh(sender, e);
}
        [BrowsableAttribute(false)]
        public new System.Drawing.Color BackColor
        {
            get { return BackColor; }//Here back color is just an example of a property, not necessarily one that I would make non-Browsable
            set { 
base.BackColor = value; 
OnPropertyChanged(this,EventArgs.Empty);
}
        }
    }
}

上面的类让我有一个属性网格,它具有图表的所有属性,并允许我在我认为合适的时候隐藏这些属性。但是,现在我一直在理解如何将我连接chart1到我的属性网格。一个例子是我已经从网格中删除了 text 属性。它不再对用户可见。现在我想能够说BackColor网格中的变化,这意味着我的chart1背景颜色发生了变化。

4

1 回答 1

2

好的,我使用了您的代码并执行了以下操作,现在更改属性网格中的背景颜色会更改图表的背景颜色:

表格1 -

public partial class Form1 : Form
{
    private PropertyGrid propertyGrid1;

    public Form1()
    {
        InitializeComponent();
        //propertyGrid1 = new PropertyGrid();
        propertyGrid1.CommandsVisibleIfAvailable = true;
        propertyGrid1.Text = "Graph and Plotting Options";
        propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;

        this.Controls.Add(propertyGrid1);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Text = "MY Plot Program";
        propertyGrid1.SelectedObject = chart1; 
    }
    private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
    {
        //Calling the method that will refresh my chart1 
        chart1.Invalidate();
    }
}



[DefaultPropertyAttribute("Text")]
public class MyChart : Chart 
{

    public event EventHandler PropertyChanged;
    private void OnPropertyChanged(object sender, EventArgs e)
    {
        if(PropertyChanged !=null)
        {
            PropertyChanged(sender, e);
        }
    }

    [BrowsableAttribute(false)]
    public new string Text { get; set; }


    [BrowsableAttribute(true)]
    public new System.Drawing.Color BackColor
    {
        get { return base.BackColor; }//Here back color is just an example of a property, not necessarily one that I would make non-Browsable
        set 
        { 
            base.BackColor = value; 

            OnPropertyChanged(this,EventArgs.Empty);
        }    
    }     
}
  • 此外,在您的Form1.Designer.cs文件中,您已将chart1其定义为“System.Windows.Forms.DataVisualization.Charting.Chart”,您需要在新的部分将其更改为 MyChart(需要搜索 Charting.Chart)
于 2013-02-27T16:07:22.090 回答