最初に、WPFフォームでWindowsフォームコンポーネントを使用する方法を決定する必要がありました。 これを行うには、 MSDNの指示に従って判断して、 WindowsFormsIntegrationおよびSystem.Windows.Formsへのリンクを追加する必要があります。 また、XAMLフォームドキュメントの<Window>要素と<WindowsFormsHost>要素に名前空間を追加する必要があります。これらの要素は、その後、必要なWindowsフォームコンポーネントをホストします。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"> <StackPanel> <WindowsFormsHost></WindowsFormsHost> </StackPanel> </Window>
これで、Windows Formsコンポーネントを使用する準備がすべて整いましたが、コンポーネントをフォームに実装する作業はすべて手動で行う必要があります。 System.Windows.Forms.DataVisualization.Chartingプロジェクトに接続し、名前空間をXAMLドキュメントに追加し、コンポーネント自体をフォームに追加します。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ... xmlns:dvc="clr-namespace:System.Windows.Forms.DataVisualization.Charting; assembly=System.Windows.Forms.DataVisualization"> <StackPanel> <WindowsFormsHost><dvc:Chart x:Name="chart" /></WindowsFormsHost> </StackPanel> </Window>
Microsoftの素晴らしいプロジェクトがあります。WindowsFormsのChartコンポーネントで作業する多くの例が含まれています。 ただし、Windows Formsコードのコンポーネントを生成する環境は、開発者が行ったコンポーネントの設定に基づいてInitializeComponent()メソッド自体で生成されるため、すぐに起動することはできません。 WPFフォームのコンポーネントコンフィギュレータを呼び出す方法が見つからなかったため、例のコードを使用する前に、手動で2、3行のコードを追加する必要があります。
using System.Windows.Forms.DataVisualization.Charting; ... private void Window_Loaded(object sender, RoutedEventArgs e) { // ChartArea, chart.ChartAreas.Add(new ChartArea("Default")); // , "Default" chart.Series.Add(new Series("Series1")); chart.Series["Series1"].ChartArea = "Default"; chart.Series["Series1"].ChartType = SeriesChartType.Line; // string[] axisXData = new string[] {"a", "b", "c"}; double[] axisYData = new double[] {0.1, 1.5, 1.9}; chart.Series["Series1"].Points.DataBindXY(axisXData, axisYData); }
その結果、素晴らしいスケジュールが得られ、最も重要なことには、サードパーティのライブラリはありません。
