简体   繁体   English

如何绑定到父Collection MVVM WPF中的特定项

[英]How to bind to a specific item inside a parent Collection MVVM WPF

I am making an MVVM WPF program and want to bind a childgrid to a single item in a parent collection, I am using telerik (RadGridView). 我正在制作MVVM WPF程序,并希望将子网格绑定到父集合中的单个项目,我正在使用telerik(RadGridView)。

The situation is as follows: 情况如下:

public class ItemDetails
{
    Item item {get; set;}
    string version {get; set;}
}

public class Item
{
    ObservableCollection<ItemVersion> itemVersions { get; set;}
}

public class ItemVersion
{
    string version {get; set;}
    ObservableCollection<ItemVersionSubItemVersion> subItemVersions {get; set;}
}

public class ItemVersionSubItemVersion
{
    ItemVersion parentItemVersion {get; set;}
    ItemVersion subItemVersion {get; set;}
}

I bind an ObservableCollection< ItemDetails> to my RadGridView and depending on what Version is requested in ItemDetails, I want to show all SubItemVersion members of the ItemVersion with that correct Version (Version will allways be unique and if there is none available a new ItemVersion will be created with an empty ObservableCollection< SubItems>). 我将一个ObservableCollection <ItemDetails>绑定到我的RadGridView,并根据ItemDetails中请求的版本,我想要显示ItemVersion的所有SubItemVersion成员以及正确的版本(版本将始终是唯一的,如果没有可用的新ItemVersion将使用空的ObservableCollection <SubItems>创建。

The only way I got this semi-working was by first showing all ItemVersions in Item and then going to the SubItems, but this shows irrelevant ItemVersions aswell and not just the one I need. 我进行这种半工作的唯一方法是首先在Item中显示所有ItemVersions,然后转到SubItems,但是这显示了不相关的ItemVersions,而不仅仅是我需要的那个。 So I want to skip a link in the chain, so to speak, and go straight to the SubItemVersions. 所以我想跳过链中的链接,可以这么说,直接进入SubItemVersions。

I was wondering if anyone knew how to either be able to select a certain item from a collection in xaml, somewhat like a linq query, but in xaml, or some other way to directly go to the SubItemVersions of the right ItemVersion. 我想知道是否有人知道如何能够从xaml中的集合中选择某个项目,有点像linq查询,但是在xaml中,或者其他方式直接转到正确的ItemVersion的SubItemVersions。

A few requirements and ideas I had that made it difficult for me to come up with a solution myself: 我的一些要求和想法让我很难自己想出一个解决方案:

  • The whole thing needs to stay in one view, so the user can keep a good overview of the structure (a SubItemVersion can have SubItemVersions of its own, which can get messy rather quickly). 整个事情需要保持在一个视图中,因此用户可以保持对结构的良好概述(SubItemVersion可以拥有自己的SubItemVersions,这可能会很快变得混乱)。

  • It needs to be MVVM, so it would be best if the code behind of the view is avoided, pure xaml is strongly preferred. 它需要是MVVM,所以如果避免使用视图后面的代码,最好是纯xaml。

  • I would prefer if the models do not need to be changed, but, if no other solution is available, I will have no other choice but to do so. 如果模型不需要更改,我更愿意,但是,如果没有其他解决方案,我将别无选择,只能这样做。

  • I thought of changing the following: 我想改变以下内容:

     public class ItemDetails { Item item {get; set;} string version {get; set;} } 

    to: 至:

     public class ItemDetails { Item item {get; set;} ItemVersion version {get; set;} } 

    But this would add an ItemVersion that is already in the ItemVersions collection in Item, so I am generating more (duplicate) data than necessary, correct? 但是这会添加一个已经在Item中的ItemVersions集合中的ItemVersion,所以我生成的数据超过了必要的(重复)数据,对吗? Or is this, in fact, a proper solution? 或者这实际上是一个合适的解决方案吗?

Any idea or input is welcome and greatly appreciated and if you need more information, please let me know so I can try and provide it. 欢迎任何想法或意见,非常感谢,如果您需要更多信息,请告诉我,以便我可以尝试提供。

EDIT: I solved my problem by creating a new ViewModel which contains 编辑:我通过创建一个包含的新ViewModel解决了我的问题

ItemVersion version
ItemDetails details

And created a collection of this new class to bind to the details, where version returns the right version, taken from details. 并创建了这个新类的集合来绑定细节,其中版本返回正确的版本,取自细节。 This seemed like the best solution and quite simple to achieve. 这似乎是最好的解决方案,而且很容易实现。

I solved my problem by creating a new ViewModel which contains 我通过创建一个包含的新ViewModel解决了我的问题

ItemVersion version
ItemDetails details

And created a collection of this new class to bind to the details and version, where version returns the right version taken from details. 并创建了这个新类的集合以绑定到详细信息和版本,其中版本返回从详细信息中获取的正确版本。 This seemed like the best solution and quite simple to achieve. 这似乎是最好的解决方案,而且很容易实现。

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

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