Embed WPF User Controls in WinForms Applications

Type "Winforms WPF" in a Google Search and you will be transported to the universe of WPF-WinForms rivalry. You'll find developers discussing the relevance of the two, quoting curses and praises for both. Don't be surprised if you see WPF basking in the glory that is has found so quickly, or WinForms with its aged and improved techniques walking with pride. It is impossible to predict which one will be standing atop the peak once the race starts. Both with their own pros and cons are a realm in their own.

WinForms and WPF are two different technologies in the .NET Framework Stack. Where WinForms lacks in flexibility and vibrant user interface, it makes up in maturity and total available support. However, it is seen time to time, that developers are porting applications from one technology to another, for the sake of better features and ease of development. At this time of changing trends, it is worth mentioning a very simple method. Through this, a WPF control can be created and embedded in a WinForms application. This way with maturity of WinForms, we can have the looks and flexibility of WPF.

Steps to embed WPF UserControl in WinForms:

WPFInWinForms Create a WinForms project named HostWPFControlInWinForms in Visual Studio. 1. Add ElementHost control To add ElementHost control, you would need to add System.Windows.Forms.Integration namespace. To add the reference to the namespace, right click References in Solution Explorer and select Add References. Select WindowsFormsIntegration.dll from Assemblies>Framework (in VS 2012 +) or from .NET Tab (in VS 2010). 2. Create a WPF UserControl In the Solution Explorer of the project, right click on project node and select Add>New Item. Select UserControl (WPF) and create your own UserControl named "WPFBrowseBar.xaml". A file WPFBrowseBar.xaml will open. Replace the XAML code with the code given below: XAML:



        <button>  

        </button>  


Right click the UserControl and go to code or press F7. Replace the class with the following snippet:


    public partial class WPFBrowseBar : UserControl  
    {  
        public WPFBrowseBar()  
        {  
            InitializeComponent();  
        }  
        public string FileName  
        {  
            get { return NewTextBox.Text; }  
            set { NewTextBox.Text = value; }  
        }  
        private void NewButton_Click(object sender, RoutedEventArgs e)  
        {  
            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();  
            if (openFileDlg.ShowDialog() == true)  
                this.FileName = openFileDlg.FileName;  
        }  
        public event EventHandler FileNameChanged;  
        private void NewTextBox_TextChanged(object sender, TextChangedEventArgs e)  
        {  
            e.Handled = true;  
            if (FileNameChanged != null)  
                FileNameChanged(this, EventArgs.Empty);  
        }  
    }  

3. Place WPF UserControl on ElementHost Build the solution so that the user control is visible in your ToolBox. The ToolBox contains a tab named "HostWPFControlInWinForms WPF User Controls". Drag your UserControl from the ToolBox and place it on your Form. With these simple steps you can add a WPF user control in your WinForms application HostWPFControlInWinFormsVB HostWPFControlInWinForms

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus