How to connect a custom propertyGrid with a standard control in C#?

谁都会走 提交于 2019-12-12 14:23:43

问题


I am creating a graphing program and I would like to allow the user to be able to change the look of the graph they create. Providing them with the opportunity to change the series color, data point size, ect. I am allowing them to do this through the use of a propertyGrid. Through the help of the wonderful people who use stack overflow I was able to import all the properties of a chart into my property grid, however; Now I can't figure out how to connect my chart to the propertyGrid, so when I change something in the grid the chart changes.

So far I have

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(); 
    }

The above code is for my Form. The my "MyChart" class code is

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);
                }
            }
        }
    }

The class above gets me so far as having a property grid that has all the properties of a chart and allows me to hide those properties as I see fit. However now I am stuck in understanding how to connect my chart1 to the grid I've created. If anyone has any advice on how to do that, it would be incredibly helpful.


回答1:


You have to add propertyGrid1.SelectedObject = myChartInstance;, then you have to add PropertyValueChanged event listener that will be triggered each time a user changes myChartInstance property via PropertyGrid. So, assuming that you want to redraw the chart each time the change has been done, the code should look like this:

        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);
        }



回答2:


Through the help of the great community on StackOverflow this was the answer I was able to piece together, (standing on the shoulders of giants)

   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();  
}

This is the code for the MyChart class

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);
}
        }
    }
}

And this declares chart1 to be a MyChart rather than System.Windows...Chart, in the Form1.Designer.CS

...this.chart1  = new FFT_Ploter.MyChart(); ... private FFT_Plotter.MyChart chart1;  


来源:https://stackoverflow.com/questions/15096971/how-to-connect-a-custom-propertygrid-with-a-standard-control-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!