Changing host WinForm properties from ViewModel bound to ElementHost

跟風遠走 提交于 2019-12-25 01:23:49

问题


Can anyone suggest approaches that will allow me to change the properties of a host WinForm from a WPF element in an ElementHost?

I have an MVVM application using Prism that's bound to an ElementHost. I would like to change the WinForm title, resize the WinForm and close the WinForm from within my ViewModel.

I understand receiving data from a WPF composite control described in this article but I can't see how that will work in a ViewModel.

App is the WinForm ChartWizardViewModel is the ViewModel I want to change the properties of App from. Everything else is the Prism architecture. Shell, Bootstrapper etc.

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:


I solved this with an EventAggregator as discussed in this post

Using EventAggregator to communicate between WPF in ElementHost and host WinForm



来源:https://stackoverflow.com/questions/9893589/changing-host-winform-properties-from-viewmodel-bound-to-elementhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!