I have the following scenario (I've stripped it down to a sample application):
WindowsFormsHost
form1.Show(this)
.Problem is:
.Owner
property is null.
.Owner
property which means I can't just ignore this problem. Ideally, of course, there wouldn't be any dependancy here..Owner
property is set correctly.I can understand why there's a problem here, but I'm hoping the answer to my next question might be 'yes'!
Is there anything I can do to get this to work by making changes on the WPF side of the equation?
Failing that, could anything be done on the WinForms side? (it's not beyond the realms of possibility that I could get some changes implemented there...)
Here's the code in my sample app. First the WPF side:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="700">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Name="btnAdd" Click="btnAdd_Click" Content="Add Winform"/>
<WindowsFormsHost Grid.Row="1" Name="host" ScrollViewer.CanContentScroll="False"/>
</Grid>
</Window>
public partial class MainWindow : Window
{
private WindowsFormsHost host;
public MainWindow()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
UserControl1 uc1 = new UserControl1();
WindowsFormsHost.EnableWindowsFormsInterop();
this.host.Child = uc1;
}
}
And now the WinForms side...
UserControl1 is just a user control with a button and a label on it. Here's the code:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show(this);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "this: " + this + Environment.NewLine + "parent: " + this.Parent + Environment.NewLine + "toplevelcontrol: " + this.TopLevelControl;
}
}
Form1 is just an empty form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MessageBox.Show(" this: " + this + " owner: " + this.Owner);
}
}
The Owner
shown in the message box and the TopLevelControl
shown in the label are null
when hosted in WPF, but have a value when hosted in another WinForms application.
I guess the problem here is that .Owner
is of type Form
and there's no instance of this type in the WPF application. It's hard to imagine what valid value that property could have in this scenario. It seems likely, therefore, that I need to get the code changed that's accessing the `.Owner' property in Form1.
<Window
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
>
<my:WindowsFormsHost Name="MapHost" ScrollViewer.CanContentScroll="False" SizeChanged="MapHost_SizeChanged" />
MapControl inherits System.Windows.Forms.Control
MapHost.Child = MapControl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With