Original: Using WinForm control methods in WPF
1. First add a reference to the following two DLL files: Windowsformsintegration.dll,system.windows.forms.dll.
2. Add the following in the XAML file for the WPF form where you want to use the WinForm control:
That
xmlns:wf= "Clr-namespace:system.windows.forms;assembly=system.windows.forms"
XMLNS:WFI = "Clr-namespace:system.windows.forms.integration;assembly=windowsformsintegration"
3. Within a WPF container control, such as StackPanel, you first add the host container for the WinForm control to connect WPF and WinForm,
The corresponding XAML is as follows:
<StackPanel> <Wfi:windowsformshost> <Wf:labelx:name= "Wflabel"Text= "WinForm control in this" /> </Wfi:windowsformshost> <Wfi:windowsformshost> <Wf:buttonx:name= "Wfbutton"Text= "OK"Click= "Wfbutton_click" /> </Wfi:windowsformshost> <ButtonContent= "button"Margin= "Ten"Name= "Button1"/></StackPanel>
Description:<wfi:windowsformshost></wfi:windowsformshost> is the host container for the WinForm control, where each host container can only have one WinForm control, as in the following example, Three WinForm controls are placed in three host containers, which can be set to adjust the size and layout of a property.
Note: such as the WinForm control I added above, such as when specifying its Name, must prefix x:, such as adding lable when <wf:label x:name= "Wpflabel" text= "I am WinForm control in WPF"/> Otherwise, the background code cannot be accessed.
4. If you want to access the above lable in the WPF backend code, you can use it just as you would in WinForm. If you click on a button to change lable content, the code is as follows: wpflabel.text= "content has changed";
5. Use the WinForm control Wpflabel change tag as an example to illustrate background usage:
The full background code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Shapes;namespacechangedetection{/// <summary> ///the interactive logic of Window4.xaml/// </summary> Public Partial classWindow4:window { PublicWindow4 () {InitializeComponent (); } Private voidWfbutton_click (Objectsender, EventArgs e) {Wflabel.text="content has changed"; } }}
The complete XAML is as follows:
<Windowx:class= "Changedetection.window4"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local= "Clr-namespace:changedetection"XMLNS:WF= "Clr-namespace:system.windows.forms;assembly=system.windows.forms"Xmlns:wfi= "Clr-namespace:system.windows.forms.integration;assembly=windowsformsintegration"mc:ignorable= "D"Title= "Window4"Height= "+"Width= "+"> <StackPanel> <Wfi:windowsformshost> <Wf:labelx:name= "Wflabel"Text= "WinForm control in this" /> </Wfi:windowsformshost> <Wfi:windowsformshost> <Wf:buttonx:name= "Wfbutton"Text= "OK"Click= "Wfbutton_click" /> </Wfi:windowsformshost> <ButtonContent= "button"Margin= "Ten"Name= "Button1"/> </StackPanel></Window>
Using WinForm control methods in WPF