2014-09-01 137 views
2

我有,我用这种方式创建我的Windows窗体视图模型:是否可以在MVVM模式中使用WindowsFormsHost?

System.Windows.Forms.DataVisualization.Charting.Chart chart = new System.Windows.Forms.DataVisualization.Charting.Chart(); 

我的XAML是:

<WindowsFormsHost x:Name="host" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
       <wfCharts:Chart x:Name="MyWinformChart" > 
        <wfCharts:Chart.Series> 
         <wfCharts:Series Name="SerieGraficoExterior" /> 
        </wfCharts:Chart.Series> 
        <wfCharts:Chart.ChartAreas> 
         <wfCharts:ChartArea/> 
        </wfCharts:Chart.ChartAreas> 
       </wfCharts:Chart> 
      </WindowsFormsHost> 

但我不知道这是否是可以绑定到我的看法,因为WindowsFormsHost控件没有像ContentControl那样的Content属性,所以我不知道是否可以在MVVM模式中使用WindowsFormsHost。我做了一些WPF尝试,后者在代码中工作,但如果可能的话,我想使用MVVM模式。

谢谢。

回答

3

您可以将Windows窗体控件嵌入到WPF应用程序中,但不能从窗体控件执行绑定。表单控件没有datacontext属性或依赖项属性,这是绑定的主干。这就是说,您仍然可以在MVVM应用程序中使用表单主机,除了表单控件(手动需要执行管道工作)之外的所有其他功能。因此,除非您的应用程序的主要部分基于Windows窗体,否则您仍然可以从MVVM中获益。

+0

所以要创建图表(Windows窗体)我需要把代码放在代码隐藏? – 2014-09-01 08:41:44

+1

当你把它放在XAML中时,控件也被创建。您可以从代码隐藏的变量名称(MyWinformChart)访问窗体控件​​, – sondergard 2014-09-01 08:44:51

相关问题