从绑定到ElementHost的ViewModel更改主机WinForm属性

时间:2012-03-27 16:20:58

标签: wpf prism winforms-interop

任何人都可以建议允许我从ElementHost中的WPF元素更改主机WinForm属性的方法吗?

Prism Stack

我有一个使用Prism绑定到ElementHost的MVVM应用程序。我想更改WinForm标题,调整WinForm的大小并从我的ViewModel中关闭WinForm。

我理解从this article中描述的WPF复合控件接收数据,但我看不出它在ViewModel中是如何工作的。

App是WinForm ChartWizardViewModel是我想要更改App的属性的ViewModel。 其他一切都是Prism架构。 Shell,Bootstrapper等。

Public Class App
Public Sub New(ByVal modulesToLoad As List(Of String))
        ' This call is required by the designer.
        InitializeComponent()

        If Application.Current Is Nothing Then
            Dim wpfAppAdapter As New WpfApplicationAdapter
        End If

        '   Load the application modules
        Dim formBootstrapper As New Bootstrapper(modulesToLoad)
        formBootstrapper.Run()

        '   Get the current instance of shell and bind it to the ElementHost
        Dim shellElement = formBootstrapper.Container.Resolve(Of Shell)()
        ehMaster.Child = shellElement
End Sub
End Class

Public NotInheritable Class Bootstrapper
Inherits UnityBootstrapper

Private _modulesToLoad As List(Of String)   '   The modules that we want to load

Public Sub New(ByVal modulesToLoad As List(Of String))
    _modulesToLoad = modulesToLoad
End Sub

Protected Overrides Function CreateShell() As DependencyObject
    Dim shell = Container.Resolve(Of Shell)()
    Return shell
End Function

Protected Overrides Function GetModuleCatalog() As IModuleCatalog
    Dim catalog As ModuleCatalog = New ConfigurationModuleCatalog()
    For Each moduleToLoad As String In _modulesToLoad
        Select Case StringHelper.CleanString(moduleToLoad)
            Case "chartwizardmodule"
                catalog.AddModule(GetType(ChartWizardModule))
        End Select
    Next
    Return catalog
End Function
End Class

Public NotInheritable Class ChartWizardModule
Implements IModule

Private ReadOnly regionManager As IRegionManager

Public Sub Initialize() Implements IModule.Initialize
    regionManager.RegisterViewWithRegion("MainRegion", GetType(MainWindow))
End Sub

Public Sub New(regionManager As IRegionManager)
    Me.regionManager = regionManager
End Sub
End Class

Partial Public Class MainWindow
Private _objChartWizardViewModel As ChartWizardViewModel    '   The chart wizard base view model that controls the rest of the views

Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Dim objChartWizardViewModel As New ChartWizardViewModel()
    _objChartWizardViewModel = objChartWizardViewModel
    '   Data Context for the Chart Wizard
    Me.DataContext = _objChartWizardViewModel
End Sub
End Class

Public Class ChartWizardViewModel
Implements INotifyPropertyChanged
    '   I need to change the properties of the WinForm (App) from here
End Class

1 个答案:

答案 0 :(得分:0)

我用这篇文章中讨论的EventAggregator解决了这个问题

Using EventAggregator to communicate between WPF in ElementHost and host WinForm