从Viewmodel访问视图 [英] Accessing View from Viewmodel

查看:82
本文介绍了从Viewmodel访问视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道设计不好,但是我需要从我的视图模型访问该视图。这是因为我有一些旧控件,例如Winforms控件不支持绑定,需要用代码填充。

I know that's bad design, but I need access to the view from my viewmodel. This is because I have some old controls, e.g. Winforms controls, that don't support binding and need to be filled by code.

我正在使用AvalonDock 2.0的MVVM模型,并且具有类似以下内容: / p>

I'm using the MVVM model of AvalonDock 2.0 and have something similar to this:

   <ad:DockingManager x:Name="dockManager" 
                  DocumentsSource="{Binding Files}"
                  AnchorablesSource="{Binding Tools}"
        ActiveContent="{Binding ActiveDocument, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}">
        <ad:DockingManager.LayoutItemTemplateSelector>
            <local:PanesTemplateSelector>
                <local:PanesTemplateSelector.NavigationViewTemplate>
                    <DataTemplate>
                        <tvext:TreeViewExtended />
                    </DataTemplate>
                </local:PanesTemplateSelector.NavigationViewTemplate>
            </local:PanesTemplateSelector>
        </ad:DockingManager.LayoutItemTemplateSelector>

因此,模板NavigationViewTemplate绑定到集合工具的一项,这是我的NavigationModel类型的ViewModel

So the template NavigationViewTemplate is bound to one item of the collection Tools, which is my ViewModel of type NavigationViewModel.

我没有绑定问题,例如一个TextBox到我的viewmodel属性。但是我不知道如何从我的NavigationViewModel访问模板中的tvext:TreeViewExtended控件以填充它。

I have no problem binding e.g. a TextBox to a property of my viewmodel. But I don't know how I can get access from my NavigationViewModel to the tvext:TreeViewExtended control inside the template in order to fill it.

TIA Michael

TIA Michael

推荐答案

我建议您不要从ViewModel访问Winforms控件。将与视图相关的所有内容保留在视图中。您可以执行以下操作:

I suggest that you do not access the Winforms control from your ViewModel. Keep everything that relates to the view in the view. You can do this as follows:


  1. 创建WPF自定义控件,例如名为 TreeViewExtendedWrapper 。 (有关如何创建自定义WPF控件的简短教程,请参阅本文。)

  1. Create a WPF custom control, e.g. named TreeViewExtendedWrapper. (See this article for a short tutorial how to create custom WPF controls).

在自定义控件的控件模板内(在Themes\Generic.xaml文件中),放置Winforms控件:

Inside the control template of the custom control (in the Themes\Generic.xaml file), place your Winforms control:

<ControlTemplate TargetType="{x:Type local:TreeViewExtendedWrapper}">
    <Border Background="{TemplateBinding Background}"
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}">
        <tvext:TreeViewExtended />
    </Border>
</ControlTemplate>


  • 为需要绑定到的所有Winforms控件属性将依赖项属性添加到自定义控件中

  • Add dependency properties to your custom control for all Winforms control properties that you need to bind to the ViewModel.

    还为您需要绑定到视图模型的所有命令的依赖项属性添加到自定义控件。

    Also add dependency properties to your custom control for all commands that you need to bind to the view model.

    在自定义控件的代码后面编写C#代码,以将自定义控件的依赖项属性连接到Winforms控件的属性,事件和方法。

    Write C# code inside the code-behind of the custom control to connect the dependency properties of your custom control to the properties, events, and methods of the Winforms control.

    在您的数据模板中,将自定义控件与任何必要的数据绑定一起放置:

    Inside your data template, place your custom control with any necessary data bindings:

    <DataTemplate>
        <local:TreeViewExtendedWrapper MyProperty={Binding MyProperty}/> 
    </DataTemplate> 
    


  • 通过这种方法,您可以使用数据绑定连接ViewModel和Winforms控件,即您没有违反MVVM原理。

    With this approach, you can use data binding to connect ViewModel and Winforms control, i.e. you do not violate MVVM principles.

    这篇关于从Viewmodel访问视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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