从绑定到ElementHost的ViewModel更改主机WinForm属性 [英] Changing host WinForm properties from ViewModel bound to ElementHost

查看:171
本文介绍了从绑定到ElementHost的ViewModel更改主机WinForm属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我了解本文中所述的从WPF复合控件接收数据的方法,但我看不到它在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.

应用程序是WinForm ChartWizardViewModel是我要从其更改App属性的ViewModel. 其他一切都是棱镜架构. Shell,Bootstrapper等.

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

推荐答案

我使用本文中讨论的EventAggregator解决了这个问题

I solved this with an EventAggregator as discussed in this post

使用EventAggregator在WPF中进行通信ElementHost和主机WinForm

这篇关于从绑定到ElementHost的ViewModel更改主机WinForm属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆