如何将自定义属性网格与C#中的标准控件连接起来
本文关键字:标准 控件 连接 起来 自定义属性 网格 | 更新日期: 2023-09-27 18:01:09
我正在创建一个绘图程序,我希望用户能够更改他们创建的图形的外观。为他们提供更改系列颜色、数据点大小等的机会。我允许他们通过使用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);
}
}
}
}
上面的类让我拥有了一个属性网格,它包含了图表的所有属性,并允许我根据需要隐藏这些属性。然而,现在我陷入了理解如何将我的图表1连接到我创建的网格的困境。如果有人对如何做到这一点有任何建议,那将是非常有帮助的。
您必须添加propertyGrid1.SelectedObject = myChartInstance;
,然后添加PropertyValueChanged
事件侦听器,每次用户通过PropertyGrid
更改myChartInstance
属性时都会触发该侦听器。因此,假设每次更改后都要重新绘制图表,代码应该如下所示:
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);
}
通过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
是MyChart
而不是System.Windows...Chart
...this.chart1 = new FFT_Ploter.MyChart(); ... private FFT_Plotter.MyChart chart1;