【问题标题】:Is it possible to use WindowsFormsHost in a MVVM pattern?是否可以在 MVVM 模式中使用 WindowsFormsHost?
【发布时间】:2014-09-01 08:27:25
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: c# wpf mvvm winforms-interop


    【解决方案1】:

    您可以将 Windows 窗体控件嵌入到 WPF 应用程序中,但不能从窗体控件进行绑定。表单控件没有作为绑定主干的 datacontext 属性或依赖项属性。

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

    【讨论】:

    • 所以要创建图表(windows 窗体)我需要将代码放在代码隐藏中?
    • 不,当你把它放在 XAML 中时,控件也在创建中。您可以从其变量名称 (MyWinformChart) 的代码隐藏中访问表单控件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2013-05-09
    相关资源
    最近更新 更多