在WPF中使用WinForm控制元件方法
下面以在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
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<wf:Button x:Name="nnn" Text="確定”/>
</wfi:WindowsFormsHost>
<wfi:WindowsFormsHost>
<zedgraph:ZedGraphControl x:Name="zedGraphControl" />
</wfi:WindowsFormsHost>
<Button Content
</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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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>