如何与标准控制在C#中连接一个自定义的PropertyGrid? [英] How to connect a custom propertyGrid with a standard control in C#?

查看:408
本文介绍了如何与标准控制在C#中连接一个自定义的PropertyGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个图表程序,我想,让用户能够改变他们所创造的图形外观。有机会改变系列彩色,数据点的大小,等提供了他们。我让他们通过使用PropertyGrid中的做到这​​一点。通过谁使用堆栈溢出,我能够但是图表的所有属性导入到我的财产格,精彩的人的帮助;现在,我无法弄清楚如何我的表连接到PropertyGrid的,所以当我改变的东西在网格中的图表的变化。

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.

到目前为止,我有

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

以上code是我的形式。在我的MyChart类code是

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

上面的类打动了我,只要有一个属性网格,有一个图表的所有属性,并允许我,因为我认为合适的隐藏这些属性。不过现在我停留在理解如何我Chart1的连接到我创建的网格。如果任何人有关于如何做到这一点的任何建议,这将是难以置信的帮助。

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.

推荐答案

您必须添加 propertyGrid1.SelectedObject = myChartInstance; ,那么你就必须添加 PropertyValueChanged 将每次触发事件侦听器用户修改 myChartInstance 通过的PropertyGrid 。因此,假设你想每次变化已经做了重新绘制图表时,code应该是这样的:

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

这篇关于如何与标准控制在C#中连接一个自定义的PropertyGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆