我正在制作图表程序,我希望用户能够快速有效地自定义图表。我创建了一个属性网格,允许他们这样做。我已经用图表的属性填充它,但删除了我不希望用户有权访问的某些元素。例如,我不希望用户能够访问辅助功能选项。到目前为止我所拥有的是
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
背面颜色会发生变化。
答案 0 :(得分:2)
好的,我拿了你的代码并做了以下操作,现在更改属性网格中的背景颜色会改变图表的背景颜色:
Form1 -
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 'd(需要搜索Charting.Chart)