TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to WPF

WPF WindowsFormsHost Example

WindowsFormsHost. Many controls based on Windows Forms exist. We can use any of them in a WPF program with the WindowsFormsHost control. In it, we specify Windows Forms controls in XAML. Only one control can be nested in it.
To start, please add a "xmlns wf" attribute to the Window element. The "wf" part is just a namespace we specify—other names can instead be used. Next, drag a WindowsFormsHost control to the application.

Tip: Try adding a Windows Forms control. All the classics you know and love are available—I used a Button.

Button

Note: You must prefix the Windows Forms control's name with the "wf" namespace. This is an XML syntax feature.

Info: I specified several attributes of the Windows Forms Button. Windows Forms does not use XAML in most programs, but within WPF it does.

Example markup: XAML <Window x:Class="WpfApplication19.MainWindow" 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="MainWindow" Height="350" Width="525"> <Grid> <WindowsFormsHost HorizontalAlignment="Left" Height="59" Margin="10,10,0,0" VerticalAlignment="Top" Width="163"> <wf:Button Text="Button" Top="0" Left="0" Click="Button_Click" BackColor="AliceBlue" ForeColor="RosyBrown" Font="Consolas" FlatStyle="Flat"/> </WindowsFormsHost> </Grid> </Window> Example code: C# using System; using System.Windows; namespace WpfApplication19 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, EventArgs e) { // ... Cast as System.Windows.Forms.Button, not // System.Windows.Controls.Button. System.Windows.Forms.Button b = sender as System.Windows.Forms.Button; b.Text = "Clicked"; } } }
Only one Windows Forms control can be nested within the WindowsFormsHost. But this can be worked around by using a control such as TableLayoutPanel that can have sub-controls. In the example, I added a Click event handler.TableLayoutPanel

So: When the user clicks on the oddly-colored Windows Forms Button, the Button_Click event handler runs.

And: The Text attribute of the Button is changed to the word "Clicked." This is not a WPF Button, which uses Content.

TextButton: WPF
Discussion. This article does not cover all the complexity of WindowsFormsHost. I leave most of that up to Microsoft. It is thoughtful that Microsoft makes WPF programs support (to some extent) Windows Forms controls.

Quote: Use the WindowsFormsHost element to place a Windows Forms control within your WPF element or page.

WindowsFormsHost Class: Microsoft Docs
Summary. Based on my experiment with WindowsFormsHost, it seems that avoiding this element is an ideal choice. Using it introduces a lot of extra complexity. For example, the Button type has to be specified as one from Windows Forms.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf