Embedding a Rhino.UI.Controls.ViewportControl into a xaml form

I have been trying to embed a Rhino Viewport into my XAML form. I’m thinking the only way to do this will be to create my form with a blank square and add the ViewportControl in during runtime.

I have had a read through and play with SampleCsEtoViewportCommand which was very helpful and tried to add it to my project, this lead me to Eto and as such I have referenced in Eto.dll from the Rhino 7 WIP Folder because the NuGet package seems to have issues.

// SampleCsEtoViewportCommand

`internal class SampleCsViewportForm : Dialog
{
public SampleCsViewportForm()
{
Title = “Rhino Viewport in an Eto Control”;
Resizable = true;
var viewport_control = new
{
new Rhino.UI.Controls.ViewportControl {Size = new Size(400, 200)},
new Rhino.UI.Controls.ViewportControl {Size = new Size(400, 200)}

  };
  Content = new StackLayout
  {
    Padding = new Padding(2),
    Spacing = 5,
    HorizontalContentAlignment = HorizontalAlignment.Stretch,
    Items =
      {
        viewport_control[0],
        viewport_control[1]
      }
  };
}

}

I have a read through the Eto Wiki on GitHub and found no help in the XAML section XAML Tips & Tricks but I did find a section for how to embed Eto.Forms into another application Embed Eto Forms

However I got stuck there on trying to find more on the .ToNative() property that seems to convert an Eto Form to a Windows Form.

System.Windows.Forms.Control myEtoControlAsWinForms = eto.ToNative(true);

I assume I could replace eto with the new StackLayout I created earlier but that doesn’t seem to work.

Thanks

It is possible to “host” WindowsForms controls in a WPF XAML layout using the WPF WindowsFormsHost element. No need to use Eto I think.

See https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/walkthrough-hosting-a-windows-forms-control-in-wpf-by-using-xaml for more details.

This is very helpful, thankyou Menno!
Although it doesn’t solve everything as the Eto Control Is not a Windows Control and I need to cast/convert it somehow using the .ToNative(true)

Hey @dale, as you created the SampleCsEto and SampleCsWpf projects do you have any ideas?

Hi @csykesm

Let me know if the attached is helpful.

SampleWpfViewport.zip (26.2 KB)

– Dale

1 Like

Thanks @dale,

that sample works perfectly!
I’ll probably use this WindowsFormHost plenty more in the future too :smiley:

1 Like