在WPF窗口中嵌入WinForms图

Yar*_*ler 9 wpf integration charts graph winforms

我试图在WPF窗口中嵌入一个.NET WinForms图(Stephan Zimmermann的图形显示),在WindowsFormsHost下(我引用了System.Windows.Forms和WindowsFormsIntegration).

但是,我可以看到表单面板而不是图表.我已经在Windows窗体上运行了演示应用程序,但是当我将相同的代码传输到WPF窗口时,我看到数据已更新但未在图表上显示.

提前感谢大家,

亚龙.

bob*_*wah 19

您可以尝试以下代码,看看是否可以显示图表然后从那里开始工作?

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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

确保您参考:

%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

我无耻地复制以下链接后运行


McA*_*den 0

将图形设置为 WindowsFormsHost 对象的子对象。