How to use third-party controls in WPF, you can use the WindowsFormsHost class

Source: Internet
Author: User
Tags in domain

Allows elements of a Windows forms control to be hosted on a WPF page.

namespaces: System.Windows.Forms.Integration

Assemblies: Windowsformsintegration (in WindowsFormsIntegration.dll) XMLNS for XAML: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

If you want to reference it,

<window x:class= "Selection.window1" 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" height= "314" width= "373" >     <Grid>     <label horizontalalignment= "left" Margin= "20,25,0,0" name= "Label1" width= "35.63" height= "23.2766666666667" verticalalignment= "Top" >first</ label>     <label height= "23.2766666666667" horizontalalignment= "left" margin= "20,56,0,0" Name= "Label2" verticalalignment= "Top" width= "52.63" >Second</Label>     <windowsformshost Name = "Hostfirst" margin= "85,25,18,0" height= "23.2766666666667" verticalalignment= "Top" >        <wf:datetimepicker name= "First"/>     </WindowsFormsHost>     < WindowsFormsHost name= "Hostsecond" margin= "85,56,18,0" height= "23.2766666666667" verticalalignment= "Top" >        <wf:datetimepicker name= "Second"/>//text box is date     &Lt;/windowsformshost>     <button height= "horizontalalignment=" left "margin=" 20,100,0,0 " Name= "Compare" verticalalignment= "Top" width= "page" click= "Compareclick" >Compare</Button>      <textbox margin= "20,131,104,20" name= "info" textwrapping= "Wrapwithoverflow" acceptsreturn= "False" isreadonly= "True"/>     <button height= "All" horizontalalignment= "right" margin= "0,0,18,20" Name = "Quit" verticalalignment= "Bottom" width= "click=" Quitclick ">Quit</Button>   </Grid> </ Window>

Background 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 Gets or sets the child controls that are hosted by the WindowsFormsHost element.

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 + = "\ r \ n";

\ r means: Carriage return (acsii:13 or 0x0d), which is what we often call a hard return.

\ n means: line break (Acsii:10 or 0x0a), is what we often say the soft return.

\ n, like you make a Web page in Dreamweaver, click Enter in the source code, is to give the source code line-wrapping.
}
private int Datecompare (datetime lefthandside, datetime righthandside) {

To do

Return 42;

}

}

}

WPF Windows Forms Integration

To consolidate Windows Form in a WPF program:

1. Add windowsformsintegration and System.Windows.Forms to the references.

2. When used in XAML, to write a clear namespace, you can define these two ns.

The following two sentences are the focus, when WPF is written, you must pay attention to the reference

xmlns:wf= "Clr-namespace:system.windows.forms;assembly=system.windows.forms"

Xmlns:wfi= "Clr-namespace:system.windows.forms.integration;assembly=windowsformsintegration"

Insert Windowsformscontrol:

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

See windows.forms below also has button control (nonsense), so want to know this button and WPF common System.Windows.Controls.Button is different.

Look at the inheritance system of both:

System.Windows.Controls in Domain: Button<buttonbase<contentcontrol<control

System.Windows.Forms Domain: Button<buttonbase<control

It seems that they are all in two different spaces, without a connection.

The content defined in the two button classes is not the same, Forms.button seems to be rich in content, found that there is a DoubleClick this event, but the trial did not work, in exchange for the Windows Form program still does not work. (This event is not visible on the property interface of Visual Studio, but you can add an event handler to your code, even if it is added). Although there is no DoubleClick in Controls.button, MouseDoubleClick is work. And Forms.button seems to trigger only the Click event.

<window x:class= "Wpfapplication1.window1" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" XM lns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= "Window1" height= "" "Width=" >

<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 height= "*"/>

<rowdefinition height= "*"/>

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

These two events correspond to different event types, One is System.Windows.Input.MouseButtonEventHandler, one is System.Windows.Forms.MouseEventHandler, so the corresponding event The parameters of the handler function are also different, one is private void Button_mousedoubleclick (object sender, MouseButtonEventArgs e), and one is private void Button_mousedoubleclick (object sender, System.Windows.Forms.MouseEventArgs e), so two event handler function names can be the same.

How to use third-party controls in WPF, you can use the WindowsFormsHost class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.