2

So I'm pretty new to WPF and MVVM, and while I understand the premise, a lot of this stuff is like trying to read hieroglyphs for me.

Basically, my situation is this: I'm using Activiz, a c# wrapper for VTK, which is an image processing/visualization library. So, in this library, there's a WinForms control called vtk:RenderWindowControl, which is an opengl control containing the class that handles all of the visualization functionality. I think it'd be easier to just use WinForms, but that's not really an option for me.

So, to use vtk:RenderWindowControl in a WPF application, I just need to shove it into a WindowsFormsHost and then I can use it more or less just like the example code, in the code behind (if that's the correct term for the .xaml.cs file)

That's fine for a test app, but in practice, I'd like to follow MVVM if possible. This is where I've run into a wall. If "renderControl" lives in the View class, how can I reference it and use it from the ViewModel? I think binding is the answer to that question, but I only really know how to do that for simple types and commands.

Following ideas in another thread I found, I managed to set up something like this answer

My codebehind looks like this:

public partial class RenderPanel_View : UserControl
{
    public static readonly new DependencyProperty RWControlProperty =
        DependencyProperty.Register("RWControl", typeof(RenderWindowControl), typeof(RenderPanel_View), new PropertyMetadata(null));

    public RenderWindowControl RWControl
    {
        get { return (RenderWindowControl)GetValue(RWControlProperty); }
        set { SetValue(RWControlProperty, value); }
    }

    public RenderPanel_View()
    {
        // This is necessary to stop the rendercontrolwindow from trying to load in the 
        // designer, and crashing the Visual Studio. 
        if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) {
            this.Height = 300;
            this.Width = 300;
            return;
        }

        InitializeComponent();
        this.RWControl = new RenderWindowControl();
        this.RWControl.Dock = System.Windows.Forms.DockStyle.Fill;
        this.WFHost.Child = this.RWControl;
    }
}

My .xaml looks like this

<UserControl x:Class="vtkMVVMTest.RenderPanel.RenderPanel_View"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
         xmlns:rp="clr-namespace:vtkMVVMTest.RenderPanel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         RWControl="{Binding VMControlProperty}">
    <Grid>
        <WindowsFormsHost x:Name ="WFHost"/>
    </Grid>
</UserControl>

So two things. One, That last line of the xaml header is an error, "The member 'RWControl' is not recognized or accessible". I don't really understand why. Second, for what I'm guessing is the ViewModel half of the equation, how is VMControlProperty supposed to be defined?

Am I at least on the right track here, or is this way off base?

Community
  • 1
  • 1
user3765410
  • 101
  • 6

1 Answers1

0

Some controls are not MVVM friendly and you have make the ViewModel aware of View interface and allow interact with it directly. Do not open the whole control to the ViewModel it will ruin the ability to write tests, put an interface on top for example IRenderPanelView and open in the interface only the functionality you need to access from ViewModel. You can then create a DP property of this type in the view, set it in the constructor and bind it to ViewModel.View property in xaml.

Alex des Pelagos
  • 1,170
  • 7
  • 8