Search code examples
wpfmvvmzedgraphwindowsformshost

Using a complex Winforms control like ZedGraph in WPF with MVVM design


I am building a WPF desktop application built on the MVVM pattern.

I want to display a number of graphs, and have decided on the old ZedGraph library, as I am familiar with it and there does not seem to be a better option available at the moment (please comment if there is)

I can put the graph control on my View with the following code:

    <WindowsFormsHost>
        <zed:ZedGraphControl x:Name="graphControl"/>
    </WindowsFormsHost>

However, I want to manipulate the graph control quite a bit, setting the axes and the data etc etc. Sticking to MVVM, I can't call the graph control from the ViewModel, and I don't want to put graph manipulation in the View's code behind.

Is there a way to put the Graph Control object in the ViewModel, manipulate it there, and still have the View display it?


Solution

  • IMO, I think it is not possible, since the underlying control which you make use is a Windows Forms control. So as a workaround, you could write a Custom WPF Control, which merely wraps Graph control using WindowsFormsHost approach and exposes the much needed properties to manipulate axes, data, etc.