Embedding WinForms Graph in WPF Window

前端 未结 4 1539
你的背包
你的背包 2020-12-17 18:42

I\'ve tried to embed a .NET WinForms graph (Stephan Zimmermann\'s Graph Display) in a WPF window, under a WindowsFormsHost (I\'ve referenced both System.Windows.Forms and Wi

相关标签:
4条回答
  • 2020-12-17 19:20

    Although the question is more than 6 years old I had a similar (if not the same issue), when trying to create and add the Chart object at runtime. Thanks to Bobwah's suggestion I could isolate the problem and found that I simply had to add a ChartArea to the Chart object to see the graph:

    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
    

    Hope it helps someone... ;)

    0 讨论(0)
  • 2020-12-17 19:21

    Set the graph as the child of the WindowsFormsHost object.

    0 讨论(0)
  • 2020-12-17 19:26

    I had the same issue in WPF. Luckily I got the solution.

    I observed that chart areas and series are being reset once the data source is set. It looks like a bug for me.

    So, the workaround/solution is to set the data source at very first place before adding the things like chart areas and series.

    0 讨论(0)
  • 2020-12-17 19:35

    Could you try the following code and see if you can get a graph to display and then work from there?

    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>
    

    making sure you have references to:

    %ProgramFiles%\Reference Assemblies\Microsoft\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\Client\System.Windows.Forms.dll

    I have this running after shamelessly copying the following link

    0 讨论(0)
提交回复
热议问题