【wpf,C#】wpf调用winform的chart空间,把数据显示成表格曲线

背景:用wpf想把数据在显示在图表,以一条曲线展示的时候,发现出了问题,wpf不像winform,直接就有chart控件,所以就花了点精力,学会了怎么调用chart控件,最终是为了把数据能够以图表曲线的形式展示出来。
当然,wpf还有其他显示更美观曲线图的方法,现在还没接触到。慢慢来吧,有好的方法可以告诉我一声哦

1. 添加引用

首先在你的项目引用里添加几个新的dll文件:

System.Windows.Forms.dll
WindowsFormsIntegration.dll
System.Windows.Forms.DataVisualization.dll

如图
在这里插入图片描述
在这里插入图片描述

2. xaml文件修改

在xaml文件片首添加以下代码:

  xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
   xmlns:Chr="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
        

并在文件中间添加以下代码:


   <WindowsFormsHost x:Name="WFHost_HistoryFlow" Margin="402,0,1.333,0.667" Height="239" VerticalAlignment="Bottom">
            <Chr:Chart x:Name="chart"/>
        </WindowsFormsHost>

该代码解释;由于wpf没有chart控件,所以以上代码就是给一个chart图表,大小都是自己设置。这里我把这个图表命名为 chart。
总体代码如图所示:
在这里插入图片描述

2. xaml.cs 文件修改

xaml.cs文件改成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data;

namespace UsingChart
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
       
            float[] X = new float[10];
            float[] Y = new float[10];
            for (int i = 0; i < 10; i++) {
                X[i] = i;
                Y[i] = i * i;
            }
            ChartArea ca0 = new ChartArea("ChartArea0");
            this.chart.ChartAreas.Add(ca0);
            //ChartArea ca1 = new ChartArea("ChartArea1");
            //this.chart.ChartAreas.Add(ca1);
            this.chart.ChartAreas[0].CursorX.IsUserEnabled = true;
            this.chart.ChartAreas[0].CursorX.AutoScroll = true;
            this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            this.chart.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
            this.chart.ChartAreas[0].AxisX.ScrollBar.Size = 10;
            this.chart.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
            this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = double.NaN;
            this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;

            this.chart.Series.Add("Series0");
            this.chart.Series[0].Points.DataBindXY(X, Y);
            this.chart.Series[0].ChartType = (SeriesChartType)3;
        }
    }
}

代码解释: 我这里自己写了一个函数 y=x^2, 其中x从0到9,所以运行后会在界面上显示这条曲线,如图所示
在这里插入图片描述
如果用你自己的数据,把数据改改,换个方式就可以啦,第二段代码也比较容易看懂,看不懂的可以跟我交流,或者有更好的方法的同伴。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF调用WinForm控件是通过将WinForm控件嵌入到WPF内容中实现的。首先,需要在WPF项目中引用`WindowsFormsIntegration`命名空间。 1. 创建WinForm控件:在WinForm项目中创建所需的WinForm控件,例如一个Windows.Forms.Button。 2. 在WPF中添加WindowsFormsHost控件:在WPF的XAML文件中,添加一个WindowsFormsHost控件。 ```xaml <Grid> <WindowsFormsHost Name="windowsFormsHost1" /> </Grid> ``` 3. 将WinForm控件添加到WindowsFormsHost控件中:在WPF的代码文件中,在相关的事件处理程序或页面加载完等事件中,将WinForm控件添加到WindowsFormsHost控件中。 ```csharp private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Forms.Button winFormButton = new System.Windows.Forms.Button(); windowsFormsHost1.Child = winFormButton; } ``` 通过以上步骤,就可以将WinForm控件嵌入到WPF中了。需要注意的是,由于WPFWinForm使用了不同的UI渲染技术,嵌入的WinForm控件的外观可能会与周围的WPF控件不完全一致,需要进行一些样式调整来保持一致性。 同时,还可以通过WPF的命令模型和事件模型与嵌入的WinForm控件进行交互。例如,可以通过WPF的命令绑定和事件处理程序来响应WinForm控件的点击事件,实现业务逻辑的处理。 总而言之,通过WindowsFormsHost控件可以在WPF中嵌入WinForm控件,并通过WPF的命令和事件模型来与其进行交互,提供更丰富的用户界面和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值