2016-08-23 115 views
0

我創建了System.Windows.Forms.DataVisualization.Charting.Chart的子類。在構造函數中我已經設置了一個默認的圖表面積和系列:繼承.NET圖表控件

System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); 
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); 

chartArea1.AxisX.Title = "Time (s)"; 
chartArea1.AxisY.Title = "Value(%)"; 
chartArea1.Name = "MainChartArea"; 
series1.ChartArea = "MainChartArea"; 
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine; 
series1.Name = "Series1"; 
Series.Add(series1); 

我已經加入的功能的集合到子類使用ChartAreas [0]。這很好。

現在在VS2010中,我創建一個新的控件,轉到工具箱並添加我的自定義圖表控件。似乎發生什麼是VS將我的默認圖表區域和系列複製到父控件的InitializeComponent函數。

當我執行代碼時,父控件的InitializeComponent函數內有一個異常,表示圖表的子類已經有一個名爲「MainChartArea」的圖表區域,並且已經有一個名爲「Series1」的系列。

如果我現在編輯父控件的InitializeComponent代碼來更改它構建的名稱並且運行得很好。

不過,如果我現在回去和默認的圖表區域,系列設置進行任何更改VS再拷貝父控件,導致了同樣的問題。

我知道我可以簡單地從父控件的InitializeComponent函數中刪除代碼,但是在圖表控件的設計器中沒有獲得可視化表示,而且手動編輯它似乎是不好的做法。

什麼是子類圖表控件,這樣我可以設置在子類中的區域和一系列正確的方法是什麼?

+2

與花哨的設計師(如Chart)一起進行控制時,如果從它們派生出來,往往會出現錯誤。他們的設計師無法分辨您在構造函數中添加的系列與您在設計師中添加的任何系列之間的區別。所以它會盡職盡責地序列化你在構造函數中創建的那個。當你在設計師中創建系列時,你不太可能想要這樣做。更糟的是,你的構造函數*也*在運行時運行,現在你有*兩個*系列你不想要的。解決這個問題需要修改設計師。完全不切實際,它是無證的,非常複雜。停止幫助吧。 –

+0

謝謝。我很害怕。 – Andy

回答

1

爲了實現你的目標,你需要從System.Windows.Forms.Design.ControlDesigner定義派生的類並分配作爲您的自定義圖表類的設計者。最初ChartAreaSeriesLegend所有創作應在ControlDesigner類的方法,而不是InitializeNewComponent的自定義圖表的構造函數來進行。

using System; 
using System.Threading.Tasks; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.Windows.Forms; 
namespace WindowsFormsApplication1 
{ 
    [System.ComponentModel.Designer(typeof(MyChartDesigner))] 
    public class MyChart : Chart 
    { 
    } 

    // Add Project Ref: System.Design 
    internal class MyChartDesigner : System.Windows.Forms.Design.ControlDesigner 
    { 
     public override void InitializeNewComponent(System.Collections.IDictionary defaultValues) 
     { 
      if ((this.Control != null) && this.Control is Chart) 
      { 
       Chart control = (Chart)this.Control; 
       if ((control.ChartAreas.Count == 0) && (control.Series.Count == 0)) 
       { 
        ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); 
        Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); 

        chartArea1.AxisX.Title = "Time (s)"; 
        chartArea1.AxisY.Title = "Value(%)"; 
        chartArea1.Name = "MainChartArea"; 
        series1.ChartArea = "MainChartArea"; 
        series1.ChartType = SeriesChartType.FastLine; 
        series1.Name = "Series1"; 

        control.ChartAreas.Add(chartArea1); 
        control.Series.Add(series1); 
       } 
      } 
      base.InitializeNewComponent(defaultValues); 
     } 
    } 
} 
1

如果您只希望能夠更改一些屬性,則解決方法可能是創建一個包含圖表的UserControl,然後向該控件添加一些屬性,以根據需要修改圖表。在實踐中,只有當你有很多共同的變化,但很少有具體的變化時,這纔會有用。

或者,你可能有一個輔助類當表單是加載,你會調用,並適用在運行時的一些變化。設計師不會顯示圖表的實際外觀。

正如在評論中提到的,當您從它們派生出來時,並非所有控件的行爲都與期望的一樣(在設計器中)。通過編寫大量代碼來處理設計時編輯和序列化可能解決一些問題,但我基本沒有經驗。

通常情況下,如果控制必須管理的子控件或列表,從控制導出可能導致問題......對於簡單的控制,往往可能只是少了幾分最優的,因爲一些屬性可能會設置兩次...