You are creating a Windows Presentation Foundation (WPF) application by using Microsoft .NET Framework 3.5.
The WPF application has a Grid control named rootGrid.
You write the following XAML code fragment.
<Window x:Class="MCP.HostingWinFormsControls" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/ presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="HostingWinFormsControls" Loaded="Window_Loaded">
<Grid x:Name="rootGrid">
</Grid>
</Window>
You need to ensure that each time the WPF window opens, a Windows Forms control named MyCustomFormsControl is added to rootGrid.
Which code segment should you use?
A.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowsFormsHost host = new WindowsFormsHost();
MyCustomFormsControl formsControl = new MyCustomFormsControl();
host.Child = formsControl;
rootGrid.Children.Add(host);
}
B.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ElementHost host = new ElementHost();
MyCustomFormsControl formsControl = new MyCustomFormsControl();
host.Child = formsControl;
rootGrid.Children.Add(host);
}
C.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MyCustomFormsControl formsControl = new MyCustomFormsControl();
formsControl.CreateControl();
HwndTarget target = new HwndTarget(formsControl.Handle);
UIElement formsElement = (UIElement)target.RootVisual;
rootGrid.Children.Add(formsElement);
}
D.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MyCustomFormsControl formsControl = new MyCustomFormsControl();
formsControl.CreateControl();
HwndSource source = HwndSource.FromHwnd(formsControl.Handle);
UIElement formsElement = (UIElement)source.RootVisual;
rootGrid.Children.Add(formsElement);
}