在WPF中使用WinForm控件方法

  • Post author:
  • Post category:其他




下面以在



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


=”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


>

转载于:https://www.cnblogs.com/liancs/archive/2011/05/12/3879317.html