WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类

摘要:
</按钮>&书信电报;使用System.Collections.Generic;使用System.Linq;使用System.Windows.Controls;使用System.Windows.Media;使用System.Windows.Media.Imageing;使用System.Windows.Navigation;

允许在 WPF 页面上承载 Windows Forms控件的元素。

命名空间:   System.Windows.Forms.Integration

程序集:   WindowsFormsIntegration(在 WindowsFormsIntegration.dll 中) 用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

add: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.integration.windowsformshost

如果要引用的话

<Window x:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"     Title="Dates" Width="373">     <Grid>     <Label HorizontalAlignment="Left" Margin="20,25,0,0" Name="label1" VerticalAlignment="Top">First</Label>     <Label HorizontalAlignment="Left" Margin="20,56,0,0" Name="label2" VerticalAlignment="Top" Width="52.63">Second</Label>     <WindowsFormsHost Name="hostFirst" Margin="85,25,18,0" VerticalAlignment="Top">       <wf:DateTimePicker Name="first"/>     </WindowsFormsHost>     <WindowsFormsHost Name="hostSecond" Margin="85,56,18,0" VerticalAlignment="Top">       <wf:DateTimePicker Name="second"/>//文本框里是日期     </WindowsFormsHost>     <Button HorizontalAlignment="Left" Margin="20,100,0,0" Name="compare" VerticalAlignment="Top" Click="compareClick">Compare</Button>     <TextBox Margin="20,131,104,20" Name="info" TextWrapping="WrapWithOverflow" AcceptsReturn="False" IsReadOnly="True" />     <Button HorizontalAlignment="Right" Margin="0,0,18,20" Name="quit" VerticalAlignment="Bottom" Click="quitClick">Quit</Button>   </Grid> </Window>

后台C#

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;

namespace Selection {   

  /// <summary>   

  /// Interaction logic for Window1.xaml     /

// </summary>
    public partial class Window1 : Window     {      

   private DateTimePicker first;        

private DateTimePicker second;
        public Window1()         {      

       InitializeComponent();

//

 Child获取或设置由 WindowsFormsHost 元素承载的子控件。      

            first = hostFirst.Child as DateTimePicker;        

     second = hostSecond.Child as DateTimePicker;         }
        private void quitClick(object sender, RoutedEventArgs e)         {         

    this.Close();     

    }
        private void compareClick(object sender, RoutedEventArgs e)         {       

      int diff = dateCompare(first.Value, second.Value);       

      info.Text = "";            

show("first == second", diff == 0);          

   show("first != second", diff != 0);          

   show("first <  second", diff < 0);            

show("first <= second", diff <= 0);           

  show("first >  second", diff > 0);           

  show("first >= second", diff >= 0);         }
        private void show(string exp, bool result)         {       

      info.Text += exp;        

     info.Text += " : " + result.ToString();      

       info.Text += " ";

 // 表示:回车符(ACSII:13 或0x0d),就是我们常说的硬回车。          

  //  表示:换行(ACSII:10 或0x0a),就是我们常说的软回车。    

         // ,好比你在DreamWeaver里做一个网页,在源代码里按一下回车,是给源代码换行。
        }
        private int dateCompare(DateTime leftHandSide, DateTime rightHandSide)         {      

       // TO DO            

return 42;   

      }   

  }

}

WPF Windows Forms integration           

 

在wpf程序中整合windows form:

1.在references中添加WindowsFormsIntegration和System.Windows.Forms。

2.在xaml中使用的时候要写清楚名字空间,可以把这两个ns定义出来。

 下面两句是重点,wpf书写的时候一定要注意引用

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

插入WindowsFormsControl:

<wfi:WindowsFormsHost>             <wf:DateTimePicker/> </wfi:WindowsFormsHost>

看到Windows.Forms下面也有Button控件(废话),于是想知道这个Button和WPF通常用的System.Windows.Controls.Button有什么不同。

看了看两者的继承体系:

System.Windows.Controls域中:  Button<ButtonBase<ContentControl<Control

System.Windows.Forms域:         Button<ButtonBase<Control

看似差不多,其实他们完全存在于两个不同的空间,没有一点联系。

两个Button类里定义的内容也不尽相同,Forms.Button看上去内容丰富一些,发现里面有DoubleClick这个event,但是试了试并不起作用,换到windows form 程序下仍然不起作用。(Visual Studio的property界面上看不到这个事件,但是可以在代码里自己加event handler,虽然加了也白加)。Controls.Button里虽然没有DoubleClick,但是MouseDoubleClick是work的。而Forms.Button似乎就只能触发Click事件。

<Window x:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1" Width="300">  

   <Grid xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"        

   xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"           >   

      <Grid.RowDefinitions>            

<RowDefinition />            

<RowDefinition />        

</Grid.RowDefinitions>   

      <Button Grid.Row="1" MouseDoubleClick="Button_MouseDoubleClick">button</Button>        

<wfi:WindowsFormsHost Grid.Row="0">            

<wf:Button MouseDoubleClick="Button_MouseDoubleClick"/>     

    </wfi:WindowsFormsHost>     </Grid> </Window>

这两个事件对应的event类型也不一样,一个是System.Windows.Input.MouseButtonEventHandler,一个是System.Windows.Forms.MouseEventHandler,所以对应的event handler的函数的参数也不一样,一个是private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e),一个是private void Button_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e),因此两个event handler函数名可以相同。

免责声明:文章转载自《WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vulkan vs OpenGL ESK8S踩坑篇-master节点作为node节点加入集群下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue 点击展开显示更多 点击收起部分隐藏

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <style ty...

jquery幻灯片插件之owl.carousel.js

官网地址:http://owlcarousel2.github.io/OwlCarousel2/ 这个插件兼容各种浏览器,以及移动端 使用方法: 1、下载文件,解压以后,把dist里面的文件放到项目中 2、引入jquery文件,必须是1.8以上的 3、页面引入的文件: <link rel="stylesheet" href="http://t.zo...

利用iis虚拟目录实现文件服务器功能

要求说明:   通过网站上传文件保存到统一的文件服务器上。   服务器说明:     1.文件服务器以下称为FilesServer,IP地址为:192.168.1.213    2.Web服务器为以下称为WebServer,IP地址为:192.168.1.214 详细步骤:   (1)在FilesServer和WebServer上分别新建一个新用户,要求这...

日志配置(springboot、mybatis、Lombok)

Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, Log4J2和Logback。每种Logger都可以通过配置使用控制台或者文件输出日志内容 SLF4J——Simple Logging Facade For Java,它是一个针对于各类Jav...

c++ 数据预处理(数据去噪,归一化)

正态分布3σ原则,把3倍方差之外的点设想为噪声数据来排除。 归一化,将数据经过处理之后限定到一定的范围内,一般都会将数据限定到[0,1]。 #include <iostream>#include <string>#include <vector>#include <algorithm>#include <...

SpringBoot入门及YML文件详解

SpringBoot 简介 微框架,与 Spring4 一起诞生,基于约定、生来为了简化 spring 的配置 优点 可以快速的上手,整合了一些子项目(开源框架或者第三方开源库) 可以依赖很少的配置快速的搭建项目 基于 spring 使开发者快速入门,门槛很低。 可以创建独立运行的应用而不需要依赖容器 提供很多 maven 极简配置,缺点是会引入很多不需...