Embedding WinForms Graphics in a WPF Window

I tried to embed the .NET WinForms graph (Stephan Zimmermann graphic display) in the WPF window under WindowsFormsHost (I referenced both System.Windows.Forms and WindowsFormsIntegration).

However, I see a form panel, but not a graph. I ran a demo application in the form of Windows, and it worked, but when I passed the same code to the WPF window, I saw that the data is being updated, but not displayed on the chart.

Thanks to everyone in advance,

Yaron.

+6
graph winforms charts wpf integration
source share
4 answers

Could you try the following code and see if you can display the graph 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 links 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 run after shamelessly copying the following link

+14
source share

Although the question is more than 6 years old, I had a similar (if not the same problem) when trying to create and add a Chart object at runtime. Thanks to Bobw's suggestion, I could isolate the problem and found that I just need to add 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 this helps someone ...;)

+1
source share

Set the graph as a child of WindowsFormsHost.

0
source share

I had the same issue in WPF. Fortunately, I got a solution.

I noticed that chart areas and rows are reset after setting the data source. This seems like a mistake to me.

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

0
source share

All Articles