I'm developing a WPF-application with MVVM. In this application I need to have an OpenGL-Control (I'm using OpenTK). The currently only useful way to get OpenGL in WPF is using a WindowsFormsHost. Until here, there's no problem.
To add content to my Scene I need access to the OpenGL-Control in my View. Of course I want to add and edit the content in the ViewModel. So, how can I get access to the OpenGL-Control without violating the MVVM-Pattern?
I'm using a scene-object which can get initialized in the View and then needs to be transferred somehow to the ViewModel. I tried this using the Tag-property of the WindowsFormsHost but without success (compare below). The property in the ViewModel is not getting updated.
Any ideas?
XAML
<UserControl x:Class="FancyOpenGlControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<WindowsFormsHost x:Name="WindowsFormsHost" Tag="{Binding Scene, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</UserControl>
C#
public FancyOpenGlControl()
{
this.InitializeComponent();
this.glControl = new OpenGLControl();
this.glControl.Dock = DockStyle.Fill;
this.WindowsFormsHost.Child = this.glControl;
this.glControl.HandleCreated += this.GlControlOnHandleCreated;
}
private void GlControlOnHandleCreated(object sender, EventArgs eventArgs)
{
this.WindowsFormsHost.Tag = new Scene(this.glControl);
// Doesn't work.
//BindingExpression bindingExpression = this.WindowsFormsHost.GetBindingExpression(TagProperty);
//if (bindingExpression != null)
//{
// bindingExpression.UpdateSource();
//}
}