是否可以在MVVM模式中使用WindowsFormsHost

本文关键字:WindowsFormsHost 模式 MVVM 是否 | 更新日期: 2023-09-27 18:20:07

我有一个视图模型,我在其中以这种方式创建窗口窗体:

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模式。

谢谢。

是否可以在MVVM模式中使用WindowsFormsHost

您可以将windows窗体控件嵌入到WPF应用程序中,但不能从窗体控件进行绑定。表单控件没有数据上下文属性或依赖项属性,这是绑定的主干。

话虽如此,您仍然可以将MVVM应用程序中的表单主机用于除表单控件之外的所有内容,在表单控件中您需要手动执行管道操作。因此,除非应用程序的主要部分基于windows表单,否则您仍然可以从MVVM中获得好处。

相关文章:
  • 没有找到相关文章