C#下WPF中调用WinForm控件

最近在C#下调用NPlot生成股票K线图,由于用的WPF框架,而NPlot是基于WinForm开发的。所以不能直接引用。具体做法如下:   NPlot源码Microsoft .NET Framework 4.5版
1、引入DLL
创建工程后需要引入WindowsFormsIntegration.dll和System.Windows.Forms.dll两个Dll。WindowsFormsIntegration.dll负责整合WPF和Windows。
2、在xaml中添加命名空间标签
xmlns : WinFormIntegration ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
3、添加宿主容器
< WinFormIntegration : WindowsFormsHost x : Name ="winFormsHost" Grid.Row ="1" Width ="Auto" Grid.ColumnSpan ="2">
      <!--在此添加WinForm控件-->
</ WinFormIntegration : WindowsFormsHost >
本例完整的xaml文件:
< Window x : Class ="AxyDeal.MainWindow"
        xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml" ;
        Title ="MainWindow" Height ="600" Width ="1080"
        xmlns : local ="clr-namespace:AxyDeal.comcontrol"
         xmlns : WinFormIntegration ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        >
    < Grid >
        < DockPanel >
            < Menu DockPanel.Dock ="Top">
                < MenuItem Header ="文件">
                    < MenuItem Header ="打开">
                        < MenuItem Header ="1.txt"></ MenuItem >
                        < MenuItem Header ="2.txt"></ MenuItem >
                        < MenuItem Header ="3.txt"></ MenuItem >
                    </ MenuItem >
                    < MenuItem Header ="退出" Name ="miExit" Click ="miExit_Click"></ MenuItem >
                </ MenuItem >
                < MenuItem Header ="编辑">
                    < MenuItem Header ="复制"></ MenuItem >
                    < MenuItem Header ="粘贴"></ MenuItem >
                </ MenuItem >
            </ Menu >
            < ToolBar DockPanel.Dock ="Top" Height ="40">
                < Button >
                    < Image Source ="images/logo_email.png"></ Image >
                </ Button >
                < Button >
                    < Image Source ="images/logo_dropbox.png"></ Image >
                </ Button >
                < Button >
                    < Image Source ="images/logo_evernote.png"></ Image >
                </ Button >
            </ ToolBar >
            < TabControl DockPanel.Dock ="Left" >
                < TabItem Header ="TabItem">
                    < Grid Background ="#FFE5E5E5"/>
                </ TabItem >
                < TabItem Header ="TabItem">
                    < Grid Background ="#FFE5E5E5"/>
                </ TabItem >
            </ TabControl >
            < Grid Height ="Auto" Width ="Auto"   Background ="AliceBlue">
                < Grid.RowDefinitions >
                    < RowDefinition Height ="Auto"></ RowDefinition >
                    < RowDefinition ></ RowDefinition >
                </ Grid.RowDefinitions >
                < Grid.ColumnDefinitions >
                    < ColumnDefinition ></ ColumnDefinition >
                    < ColumnDefinition ></ ColumnDefinition >
                </ Grid.ColumnDefinitions >
                < local : TickerControl x : Name ="tickerControl" Grid.Row ="0" Height ="50" Grid.ColumnSpan ="2" Grid.RowSpan ="1" Width ="686" VerticalAlignment ="Top" HorizontalAlignment ="Center" Visibility ="Visible" Margin ="1,0,0,0" ></ local : TickerControl >
                < WinFormIntegration : WindowsFormsHost x : Name ="winFormsHost" Grid.Row ="1" Width ="Auto" Grid.ColumnSpan ="2">
                    <!--在此添加WinForm控件-->
                </ WinFormIntegration : WindowsFormsHost >               
            </ Grid >
        </ DockPanel >
    </ Grid >
</ Window >
4、在代码中引入WinForm控件
这里声明了两个要引入的变量
//Form实例
private Form kLineForm = new Form();
//要引入的NPlot控件
private NPlot.Windows.PlotSurface2D KLinePS;
然后在MainWindow方法中添加:
//把Form添加的xaml中创建的宿主容器
this.winFormsHost.Child = this.kLineForm;
KLinePS = new NPlot.Windows.PlotSurface2D();
//把NPlot控件添加的Form中
this.kLineForm.Controls.Add(this.KLinePS);
至此可以在WPF中任意使用NPlot了。
使用NPlot是一款免费又开源.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等。
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡梦_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值