2010-03-04 76 views
6

我試圖在WindowsFormsHost(我引用了System.Windows.Forms和WindowsFormsIntegration)下的WPF窗口中嵌入.NET WinForms圖形(Stephan Zimmermann的圖形顯示)。在WPF窗口中嵌入WinForms圖形

但是,我可以看到表單面板而不是圖表。我已經在Windows窗體上運行了演示應用程序,並且它工作正常,但是當我將相同的代碼傳輸到WPF窗口時,我看到數據已更新,但未顯示在圖上。

在此先感謝大家,

Yaron。

+0

你能發佈你的WPF代碼嗎? – micahtan 2010-03-10 00:58:21

回答

0

將圖形設置爲WindowsFormsHost對象的子級。

+0

已經做到了,它沒有幫助。我仍然無法看到圖形更新。 – 2010-03-04 08:53:31

14

你可以嘗試下面的代碼,看看你是否可以得到一個圖來顯示,然後從那裏工作?

MainWindow.xaml.cs

using System.Collections.Generic; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.Windows; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     Dictionary<int, double> value; 
     public MainWindow() 
     { 
      InitializeComponent(); 

      value = new Dictionary<int, double>(); 
      for (int i = 0; i < 10; i++) 
       value.Add(i, 10 * i); 

      Chart chart = this.FindName("MyWinformChart") as Chart; 
      chart.DataSource = value; 
      chart.Series["series"].XValueMember = "Key"; 
      chart.Series["series"].YValueMembers = "Value"; 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:winformchart="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization" 
    Title="MainWindow" Height="392" Width="525"> 
    <StackPanel> 

     <WindowsFormsHost x:Name="host" Height="300"> 
      <winformchart:Chart x:Name="MyWinformChart" Dock="Fill"> 
       <winformchart:Chart.Series> 
        <winformchart:Series Name="series" ChartType="Line"/> 
       </winformchart:Chart.Series> 
       <winformchart:Chart.ChartAreas> 
        <winformchart:ChartArea/> 
       </winformchart:Chart.ChartAreas> 
      </winformchart:Chart> 
     </WindowsFormsHost> 

    </StackPanel> 
</Window> 

確保您有引用:

的%ProgramFiles%\參考大會\微軟\ Framework.NETFramework \ v4.0 \ Profile \ Client \ WindowsFormsIntegration.dll

%ProgramFiles% \ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ Profile \ Client \ System.Windows.Forms.DataVisualization.dll

%ProgramFiles%\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ Profile \客戶端\ System.Windows.Forms.dll的

我有這個運行後複製無恥以下link

1

雖然問題是超過6歲,我有一個類似(如果不是相同的問題),當試圖在運行時創建並添加Chart對象。 由於Bobwah的建議,我會找出問題,發現我只是有一個ChartArea添加到圖表對象以查看該圖:

Chart chart = new Chart(); 
chart.ChartAreas.Add("MainChartArea"); //this was missing 
chart.Series.Add(getSeries()); 
chart.Dock = System.Windows.Forms.DockStyle.Fill; 
host.Child = chart; //'host' is the WPF-WindowsFormsHost control 

希望它可以幫助別人...;)

0

我在WPF中有同樣的問題。幸運的是我得到了解決方案。

我發現一旦數據源被設置,圖表區域和系列就會被重置。它對我來說看起來像一個bug。

因此,解決方法是在添加諸如圖表區域和系列之前,將數據源設置在第一位。