I am trying to host a custom Windows Forms control in WPF. My custom control does not have a public constructor, and it has a static Create()
method which looks something like this:
public abstract class MyCustomControl : UserControl
{
internal MyCustomControl(...) { }
public static MyCustomControl Create(SomeEnum kind)
{
switch (kind)
{
case SomeEnum.Kind1:
return new MySuperCustomControl(...);
...
}
What I want to do is instantiate this custom control in WPF and then have it hosted in WindowsFormsHost
, but I obviously can't add an abstract class:
<wfi:WindowsFormsHost Width="250" Height="150">
<my:MyCust开发者_如何学JAVAomControl x:Name="customControl" /> <-- doesn't work
</wfi:WindowsFormsHost>
Is there a way I could add it into the "Host" via code?
You can't host control without public constructor in XAML. You can try two way:
- define name for your WindowsFormsHost and set Child property of WindowsFormsHost to your instance from static Create() in C# code. for example in initialize (or load) method. - it is simple way.
- try to bind Child property of WindowsFormsHost to Create() method. Frankly, I don't know or this approach will be work... but you can try :).. how bind to method in XAML? you can read - this or try to look in msdn or google :)
Found it, it's the WindowsFormsHost.Child
property.
精彩评论