3

我正在创建一个图形程序,我希望用户能够更改他们创建的图形的外观。为他们提供更改系列颜色、数据点大小等的机会。我允许他们通过使用 propertyGrid 来做到这一点。然而,通过使用堆栈溢出的优秀人员的帮助,我能够将图表的所有属性导入我的属性网格;现在我不知道如何将我的图表连接到 propertyGrid,所以当我更改网格中的某些内容时,图表会发生变化。

到目前为止我有

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)
    {
        //Getting the MyChart instance from propertyGrid 
        MyChart myChart = (MyChart)(((PropertyGrid)s.SelectedObject);
        //Calling the method that will refresh my chart1 
        myChart.Invalidate(); 
    }

上面的代码是我的表格。我的“MyChart”类代码是

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 连接到我创建的网格。如果有人对如何做到这一点有任何建议,那将非常有帮助。

4

2 回答 2

2

您必须添加propertyGrid1.SelectedObject = myChartInstance;,然后您必须添加PropertyValueChanged将在用户每次更改myChartInstance属性时触发的事件侦听器PropertyGrid。因此,假设您希望在每次更改完成后重新绘制图表,代码应如下所示:

        private void propertyGrid1_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
        {
           // Redraw the chart.
           chart1.Invalidate();
        }

        public Form1()
        {
            InitializeComponent();
            magRadioBox.Checked = true;
            PropertyGrid propertyGrid1 = new PropertyGrid();
            propertyGrid1.CommandsVisibleIfAvailable = true;
            propertyGrid1.Text = "Graph and Plotting Options";

            // Create your chart.
            chart1 = new MyChart();

            // Attach your chart to Property Grid.
            propertyGrid1.SelectedObject = (MyChart) chart1;
            propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;

            this.Controls.Add(propertyGrid1);
        }
于 2013-02-26T19:11:13.837 回答
0

通过 StackOverflow 上伟大社区的帮助,这是我能够拼凑的答案,(站在巨人的肩膀上)

   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) { 
myChart.Invalidate();  
}

这是MyChart类的代码

namespace FFT_Plotter
{ 
    [DefaultPropertyAttribute("Text")]// This is where the initial position of the grid is set 
    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);
}
        }
    }
}

这在 Form1.Designer.CS 中声明chart1为 aMyChart而不是System.Windows...Chart

...this.chart1  = new FFT_Ploter.MyChart(); ... private FFT_Plotter.MyChart chart1;  
于 2013-02-27T18:39:50.973 回答