【问题标题】:How to use a personalized property grid with a Chart class?如何使用带有 Chart 类的个性化属性网格?
【发布时间】:2013-02-27 15:17:33
【问题描述】:

我正在开发一个绘图程序,我希望用户能够快速有效地自定义他们的图表。我创建了一个允许他们这样做的属性网格。我已经用图表的属性填充了它,但删除了我不希望用户访问的某些元素。例如,我不希望用户能够访问辅助功能选项。到目前为止,我所拥有的是

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 背面颜色发生了变化。

【问题讨论】:

    标签: c# winforms charts customization propertygrid


    【解决方案1】:

    好的,我使用了您的代码并执行了以下操作,现在更改属性网格中的背景颜色会更改图表的背景颜色:

    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(需要搜索 Charting.Chart)

    【讨论】:

    • 我已经更改了 MyChart 的代码,但除此之外,我不知道您在 1,2 和 3 中的意思。我认为您的意思是 (1)private Form1(){...PropertyGrid propertyGrid1 = new PropertyGrid();} (2 )没有线索(3)this.chart1 = new MyChart(); ... private MyChart chart1; 。对于(4)我已经完成了private void propertyGrid_PropertyValueChange(...){ chart1.Invalidate()};
    • 我更新为包含除Form1.Designer.cs之外的所有代码
    • 另外,您可以使用设计器添加 PropertyGrid,而不是在构造函数中实例化并添加它。
    • 非常感谢您的时间和考虑。您提供的建议非常宝贵,让我有机会继续我的项目。除此之外,您还向我展示了一些我不知道的关于 C# 的事情。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2015-07-18
    • 2015-07-19
    相关资源
    最近更新 更多