简体   繁体   English

将多个视图绑定到多个视图模型

[英]Bind multiple views to multiple viewmodels

I have a WPF window displaying different self-defined Views. 我有一个WPF窗口显示不同的自定义视图。 So far I was able to use everything I learned about MVVM :) 到目前为止,我能够使用我学到的关于MVVM的一切:)

Now I got to a new "problem": I have 10 entities of the same view in a bigger view. 现在我遇到了一个新的“问题”:在更大的视图中我有10个相同视图的实体。 These ten view-entities contain a set of controls (textbox, combobox etc.) but are all consistent. 这十个视图实体包含一组控件(文本框,组合框等),但都是一致的。

So how do I bind these Views to a ViewModel? 那么如何将这些视图绑定到ViewModel?

I thought about having 10 instances of the ViewModel in the "higher-level" ViewModel and give the views fix-defined the instances of the VM as datacontext. 我想过在“更高级别”的ViewModel中有10个ViewModel实例,并将视图修复定义为VM的实例作为datacontext。

My question is now --> Is there a easier (or more convienient) way to bind many (identical) views to their viewmodels? 我现在的问题是 - >是否有更简单(或更方便)的方法将许多(相同的)视图绑定到其视图模型?

Code-Example: 代码示例:

View Model: 查看型号:

private PanelViewModel _panelViewModel1 = new PanelViewModel();
public PanelViewModel PanelVM1
{
   get { return _panelViewModel1; }
}  

View-Example: 视图 - 例如:

<myControls:vwPanel HorizontalAlignment="Left" x:Name="vwPanel1" 
                    VerticalAlignment="Top" DataContext="{Binding Path=PanelVM1}"/>

What bothers me is that I would need this logic ten times for ten views? 让我感到困扰的是,十次观看我需要十倍的逻辑吗?

UPDATE: To answer some questions: I want to show one view 10 times (in my example) I defined my own view by inheriting from UserControl. 更新:回答一些问题:我想要显示一个视图10次(在我的示例中)我通过继承UserControl来定义自己的视图。 So my vwPanel inherits from UserControl . 所以我的vwPanel继承自UserControl The 10 vwPanels are just placed inside a StackPanel inside a Grid. 10个vwPanel只放在Grid内的StackPanel中。

It's not about displaying data, as you pointed out, there would be a listview or a datagrid a better place to start. 这不是关于显示数据,正如您所指出的那样,列表视图或数据网格是一个更好的起点。 It's a special case where I need this much input-controls :/ 这是一个特殊情况,我需要这么多输入控件:/

UPDATE2: What I hoped for was more like defining a List of ViewModels and Bind my 10 Views to one of this List. UPDATE2:我希望更像是定义ViewModel列表并将我的10个视图绑定到此列表中的一个。 But this will not work will it? 但这不会起作用吗? At least I wouldn't know how to refernce one "special" entitiy in the list out of XAML... 至少我不知道如何在XAML列表中引用一个“特殊”权利......

Typically I use implicit DataTemplates for mapping Views to ViewModels . 通常我使用隐式DataTemplatesViews映射到ViewModels They can go in <Application.Resources> , <Window.Resources> or even in under specific elements only such as <TabControl.Resources> 它们可以进入<Application.Resources><Window.Resources> ,甚至可以进入特定元素,例如<TabControl.Resources>

<DataTemplate DataType="{x:Type local:PanelViewModel}">
    <myControls:vwPanel />
</DataTemplate>

This means that anytime WPF encounters an object in the VisualTree of type PanelViewModel , it will draw it using vwPanel 这意味着任何时候WPF遇到PanelViewModel类型的VisualTree中的对象,它将使用vwPanel绘制它

Objects typically get placed in the VisualTree through an ItemsSource property 对象通常通过ItemsSource属性放置在VisualTree

<ItemsControl ItemsSource="{Binding CollectionOfAllPanels}" />

or by using a ContentControl 或者使用ContentControl

<ContentControl Content="{Binding PanelVM1}" />

If I understand your question correctly, you have a collection of something that you what to represent visually. 如果我正确地理解了您的问题,那么您可以收集一些可视化表示的内容。 That is, you have several viewmodels that you want to define a single view for, but show X number of times. 也就是说,您有几个要为其定义单个视图的视图模型,但显示X次。 Your example shows you using a panel as your view for the "PanelViewModel"...what is the parent item's control for the vwPanel? 您的示例显示您使用面板作为“PanelViewModel”的视图...什么是vwPanel的父项控件? Assuming you're using something like a ListBox, you can define a custom DataTemplate that contains your vwPanel and assign that DataTemplate to your ListBox.ItemTemplate. 假设您正在使用类似ListBox的东西,您可以定义包含vwPanel的自定义DataTemplate,并将该DataTemplate分配给ListBox.ItemTemplate。

For example: 例如:

<Window.Resources>
    <DataTemplate x:Key="myVMTemplate" TargetType="{x:Type myViewModels:PanelViewModel}">
        <myControls:vwPanel HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </DataTemplate>
</Window.Resources>

<ListBox ItemsSource="{Binding Path=MyCollectionOfPanelVMs}" 
         ItemTemplate="{StaticResource myVMTemplate}" />

I haven't verified that this works. 我还没有证实这是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM