轉帖|其它|編輯:郝浩|2011-05-13 14:44:52.000|閱讀 1221 次
概述:下面以在Wpf中添加ZedGraph(用于創建任意數據的二維線型、條型、餅型圖表的一個開源類庫)控件,說明在WPF中使用Winform控件的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
下面以在Wpf中添加ZedGraph(用于創建任意數據的二維線型、條型、餅型圖表的一個開源類庫)控件,說明在WPF中使用Winform控件的方法。
1、 首先添加對如下兩個dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll。
2、 由于要用到ZedGraph控件,所以也要添加對ZedGraph.dll的引用。
3、 在要使用WinForm控件的WPF窗體的XAML文件中添加如下內容(選中部分):
即:
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;
assembly=System.Windows.Forms"
xmlns:zedgraph="clr-namespace:ZedGraph;assembly=ZedGraph"
4、 在WPF的容器控件內如Grid內首先要添加WinForm控件的宿主容器,用于銜接WPF和WinForm,
對應XAML如下:
<Grid>
<wfi:WindowsFormsHost Width="300" HorizontalAlignment="Right">
<wf:Label x:Name="lblName" Text="winForm控件在此” />
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<wf:Button x:Name="nnn" Text="確定”/>
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<zedgraph:ZedGraphControl x:Name="zedGraphControl" />
</wfi:WindowsFormsHost>
<Button Content="Button" Height="23" HorizontalAlignment="Left"
Margin="10,10,0,0"
Name="button1" VerticalAlignment="Top"
Width="75" Click="button1_Click" />
</Grid>
說明:<wfi:WindowsFormsHost></wfi:WindowsFormsHost>即為WinForm控件的宿主容器,每一個宿主容器只能放一個WinForm控件,如下例,放了三個WinForm控件,分別放在三個宿主容器里面,該容器可以設置屬性來調整大小和布局。
注意:如上我添加的WinForm控件如在指定其Name時,必須加前綴x:,如添加Lable時<wf:Label x:Name="lblName" Text="我是WPF中的WinForm控件” />,
否則后臺代碼無法訪問。
5、 如果要在WPF后臺代碼中訪問上面的Lable,可直接像在WinForm中使用一樣。如在點擊某一按鈕時改變Lable內容,代碼如下:lblName.Text=”內容已改變”;
6、 以使用WinForm控件ZedGraph繪制曲線為例,說明后臺用法:
在后臺用Using添加對System.Windows.Forms和ZedGraph和System.Drawing命名空間的引用,完整后臺代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 ZedGraph;
using System.Drawing;
namespace ZedGraphAtWpf2
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
lblName.Text =”我是WinForm控件”;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ZedGraph.GraphPane myPane = zedGraphControl.GraphPane;
myPane.Title.Text = "My Test Graph\n(For CodeProject Sample)";
myPane.XAxis.Title.Text = "My X Axis";
myPane.YAxis.Title.Text = "My Y Axis";
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
for (int i = 0; i < 36; i++)
{
double x = (double)i + 5;
double y1 = 1.5 + Math.Sin((double)i * 0.2);
double y2 = 3.0 * (1.5 + Math.Sin((double)i * 0.2));
list1.Add(x, y1);
list2.Add(x, y2);
}
// Generate a red curve with diamond
// symbols, and "Porsche" in the legend
LineItem myCurve = myPane.AddCurve("Porsche",
list1, System.Drawing.Color.Red, SymbolType.Diamond);
// Generate a blue curve with circle
// symbols, and "Piper" in the legend
LineItem myCurve2 = myPane.AddCurve("Piper",
list2, System.Drawing.Color.Blue, SymbolType.Circle);
zedGraphControl.AxisChange();
}
}
}
完整XAML如下:
<Window x:Class="ZedGraphAtWpf2.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:zedgraph="clr-namespace:ZedGraph;assembly=ZedGraph"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<wfi:WindowsFormsHost Width="300" HorizontalAlignment="Right">
<wf:Label x:Name="lblName" Text="WinForm控件在此” />
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<wf:Button x:Name="nnn" Text="確定” />
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<zedgraph:ZedGraphControl x:Name="zedGraphControl" />
</wfi:WindowsFormsHost>
<Button Content="Button" Height="23" HorizontalAlignment="Left"
Margin="10,10,0,0"
Name="button1" VerticalAlignment="Top"
Width="75" Click="button1_Click" />
</Grid>
</Window>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:CSDN