加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在WPF中使用WinForm控件方法

(2016-08-17 20:12:27)
标签:

wpf

分类: WPF

下面以在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如下:

 

        

            

        

        

    

说明:即为WinForm控件的宿主容器,每一个宿主容器只能放一个WinForm控件,如下例,放了三个WinForm控件,分别放在三个宿主容器里面,该容器可以设置属性来调整大小和布局。

注意:如上我添加的WinForm控件如在指定其Name时,必须加前缀x:,如添加Lable时

            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如下:

 

         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">

    

        

            

        

        

    

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有