WPF Interview Questions

By | April 28, 2014

Windows Presentation Foundation Interview Questions:

Q: How do I host a Windows Forms control in a WPF application? A: First make sure to add references to System.Windows.Forms and System.Windows.Forms.Integration. Then you need to decide if you will use code, XAML or a combination of both to work with the Windows Forms controls. If you are strictly using code, you would write code similar to the following: //Instantiate the hosting control WindowsFormsHost host = new WindowsFormsHost(); //Instantiate the Windows Forms control, in this case a button System.Windows.Forms.Button wfButton = new System.Windows.Forms.Button(); wfButton.Text = “Windows Forms Button”; // Add the Windows Forms button to the host control host.Children.Add(wfButton); // Add the host control to the WPF element that you want to parent the control, // in this case it’s a Grid this.grid1.Children.Add(host); If you are using XAML, you still need to add the references to System.Windows.Forms and System.Windows.Forms.Integration, but you will also need to add mapping statements to your XAML that will allow you to refer to the objects that live in these namespaces via XAML: <?Mapping XmlNamespace=”wfi” ClrNamespace=”System.Windows.Forms.Integration” Assembly=”WindowsFormsIntegration”?> <?Mapping XmlNamespace=”wf” ClrNamespace=”System.Windows.Forms” Assembly=”System.Windows.Forms”?> The XmlNamespace property provide you a way to create a tag that you can use as namespace prefix in the XAML to refer to the controls within the System.Windows.Forms and System.Windows.Forms.Integration namespaces. To enable this, you must also create xmlns properties in the XAML that map back to these prefixes: <Windowx:Class=AvalonApplication17.Window1 xmlns=http://schemas.microsoft.com/winfx/avalon/2005 xmlns:x=http://schemas.microsoft.com/winfx/xaml/2005 xmlns:wfi=wfi xmlns:wf=wf Title=AvalonApplication17 Loaded=WindowLoaded > Then you can use XAML to instantiate the WindowsFormsHost control and its subsequent child controls: <Gridx:Name=grid1> <wfi:WindowsFormsHost> <wf:ButtonText=Windows Forms Button/> </wfi:WindowsFormsHost> </Grid>

Q: I can’t seem to find System.Windows.Forms.Integration, where do I find it?

A: The System.Windows.Forms.Integration namespace is defined in WindowsFormsIntegration.dll which currently ships in the WinFX SDK, not in the standard redist. Therefore, the file will be found in “\Program Files\Reference Assemblies\ Microsoft\Avalon\v2.0.50215”.

Q: How do I host a WPF control in a Windows Forms application? A: First add references to the WPF namespaces (PresentationCore, PresentationFramework, UIAutomationProvider, UIAutomationTypes, and WindowsBase). Next create an instance of the ElementHost control and the control you wish to embed in the Windows Forms application and then hook that control up to the ElementHost control. Then simply add the ElementHost control to your Forms control collection: ElementHost host = new ElementHost(); System.Windows.Controls.ListBox wpfListBox = new System.Windows.Controls.ListBox(); for (int i = 0; i < 10; i++) { wpfListBox.Items.Add(“Item “ + i.ToString()); } host.Dock = DockStyle.Fill; host.Controls.Add(wpfListBox); this.panel1.Controls.Add(host); However, if you want to use XAML to describe the WPF control that you want to use in the Windows Forms application, you would need to add an Avalon UserControl item to your project. This will create a UserControl1.xaml file and a UserControl1.xaml.cs file. You can then modify the UserControl1.xaml file to contain whatever XAML you wish to describe your control. Then you would simply create an instance of this control and add it to the ElementHost control as in the above example: ElementHost host = new ElementHost(); UserControl1 uc1 = new UserControl1(); host.Controls.Add(uc1); host.Dock = DockStyle.Fill; this.panel1.Controls.Add(host); In addition, you will need to modify the project file because the Windows Application does not what to do with the XAML file. You will need to open the project file (.csproj, .vbproj, etc.) in an editor like Notepad and then scroll to the bottom. You will see the following line: <Import Project=”$(MSBuildBinPath)\Microsoft.CSharp.targets” /> You will need to copy this line and paste it just below the above line and then change “CSharp” to “WinFX” so that the two lines look like: <Import Project=”$(MSBuildBinPath)\Microsoft.CSharp.targets” /> <Import Project=”$(MSBuildBinPath)\Microsoft.WinFx.targets” /> Now save this file and reload the project using VS and run the application.

Q: Can I use Windows Forms in a WPF application?

A: Absolutely. You can have a WPF application pop a Windows Form much in the same way that you can pop a WPF window from a Windows Forms application. Furthermore, you can place Windows Forms controls side-by-side with WPF controls on a WPF window or page by using the WindowsFormsHost control that will ship as part of the interoperability layer.

Q: What about user controls and third-party controls including ActiveX controls?

A: Windows Forms user controls will work the same as the standard Windows Forms controls, which means you can certainly use them in WPF applications. As far as third-party controls go, it depends on how these controls are built. If they are built as managed Windows Forms custom controls they will also work fine in this scenario. If they are built as ActiveX controls they can also be used in a WPF application as long as they are contained within a WindowsFormsHost control. Since Windows Forms already has support for hosting ActiveX controls, all you need to do is generate the managed wrappers for the ActiveX control and then use those wrappers to instantiate the control and host it within the WindowsFormsHost control on a WPF window or page. One handy way to do this is to simply create a Windows Forms user control that contains the ActiveX control and then simply host the user control in the WindowsFormsHost control.

Leave a Reply