title: WPF MVVM WinformsHost with OpenGL-Control
body: <p>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.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;UserControl x:Class="FancyOpenGlControl"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;    
      &lt;WindowsFormsHost x:Name="WindowsFormsHost" Tag="{Binding Scene, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>C#</strong></p>
  
  <pre><code>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();
      //}
  }
  </code></pre>