0

谁能建议允许我从 ElementHost 中的 WPF 元素更改主机 WinForm 的属性的方法?

棱镜堆栈

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

我了解从本文中描述的 WPF 复合控件接收数据,但我看不出这将如何在 ViewModel 中工作。

App 是 WinForm ChartWizardViewModel 是我想要更改 App 属性的 ViewModel。其他一切都是棱镜架构。壳牌,引导程序等。

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
4

1 回答 1

0

如本文所述,我使用 EventAggregator 解决了这个问题

使用 EventAggregator 在 ElementHost 中的 WPF 和宿主 WinForm 之间进行通信

于 2012-03-29T19:38:44.710 回答