C#下WPF中调用WinForm控件
来源:互联网 发布:怪物猎人ol激活码淘宝 编辑:程序博客网 时间:2024/05/16 16:19
最近在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:WindowsFormsHostx:Name="winFormsHost"Grid.Row="1"Width="Auto"Grid.ColumnSpan="2">
<!--在此添加WinForm控件-->
</WinFormIntegration:WindowsFormsHost>
本例完整的xaml文件:
<Windowx:Class="AxyDeal.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
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>
<MenuDockPanel.Dock="Top">
<MenuItemHeader="文件">
<MenuItemHeader="打开">
<MenuItemHeader="1.txt"></MenuItem>
<MenuItemHeader="2.txt"></MenuItem>
<MenuItemHeader="3.txt"></MenuItem>
</MenuItem>
<MenuItemHeader="退出"Name="miExit"Click="miExit_Click"></MenuItem>
</MenuItem>
<MenuItemHeader="编辑">
<MenuItemHeader="复制"></MenuItem>
<MenuItemHeader="粘贴"></MenuItem>
</MenuItem>
</Menu>
<ToolBarDockPanel.Dock="Top"Height="40">
<Button>
<ImageSource="images/logo_email.png"></Image>
</Button>
<Button>
<ImageSource="images/logo_dropbox.png"></Image>
</Button>
<Button>
<ImageSource="images/logo_evernote.png"></Image>
</Button>
</ToolBar>
<TabControlDockPanel.Dock="Left" >
<TabItemHeader="TabItem">
<GridBackground="#FFE5E5E5"/>
</TabItem>
<TabItemHeader="TabItem">
<GridBackground="#FFE5E5E5"/>
</TabItem>
</TabControl>
<GridHeight="Auto"Width="Auto" Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinitionHeight="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<local:TickerControlx: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:WindowsFormsHostx: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平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等。
阅读全文
0 0
- C#下WPF中调用WinForm控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- 如何在WPF中调用Winform控件
- C# WinForm中使用WPF的控件
- C# Winform使用WPF控件
- WPF中使用winform 控件
- WPF调用Winform控件具体技巧
- 如何在WPF中调用C#控件库(HexEdit)
- 在WinForm应用程序中嵌入WPF控件
- 在WinForm应用程序中嵌入WPF控件
- 在WPF中使用WinForm控件方法
- 在WinForm应用程序中嵌入WPF控件
- 在WPF中使用WinForm控件方法
- 在WPF中嵌入winform控件
- 如何在WPF中嵌入Winform控件
- Android报错集锦之一:You need to use a Theme.AppCompat theme (or descendant) with this activity.
- c++11通过变参模板实现特殊的数据结构和算法
- spring和mybactis的整合
- Glide使用中遇到的问题
- 时间处理
- C#下WPF中调用WinForm控件
- V-layout自我学习总结(自我记录)
- cocos2dx 截取屏幕保存至手机相册中
- 升序和降序操作
- Linux-HA 入门指南(引用)
- C++中this指针的用法详解
- Gradle快速
- HashMap源码解读
- leetcode49. Group Anagrams