1

私はグラフ作成プログラムに取り組んでおり、ユーザーにグラフをすばやく効果的にカスタマイズしてもらいたいと思っています。私は彼らがそれを行うことを可能にするプロパティグリッドを作成しました。グラフのプロパティを入力しましたが、ユーザーにアクセスさせたくない特定の要素を削除しました。たとえば、ユーザーがユーザー補助オプションにアクセスできるようにしたくありません。これまでのところ私が持っているのは

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バックカラーが変化することを意味します。

4

1 に答える 1

2

わかりました。コードを取得して次のようにしました。プロパティグリッドの背景色を変更すると、グラフの背景色が変更されます。

フォーム1-

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を検索する必要があります)。
于 2013-02-27T16:07:22.090 に答える