MVVM:在(Silverlight)中管理与树视图的多对多关系

发布于 2024-11-05 02:03:29 字数 1509 浏览 4 评论 0原文

很抱歉问一个广泛的问题,但我无法思考如何解决这个问题。

我有一个多对多的关系:

Product 1 --- * ProductCategory * --- Category

类别还有:

Category 0..1 --- * Category (As SubCategories)

我想要查看包含所有类别的树视图,并且能够选中复选框来创建 ProductCategory 实体。我不知道如何使用 MVVM 来解决这个问题。

在此处输入图像描述

在 ASP.NET 中,渲染树视图时,我会检查节点的 CategoryId 是否为 I渲染是否在我的“ProductCategory”列表中(如果是),我会选中该框,然后设置初始状态。

然后,我将事件处理程序附加到节点(可能还有一些数据),以便当选中/取消选中复选框时,它会从我的列表中添加/删除适当的实体。

问题是我正在尝试转向 MVVM,有时会看到它到底有什么帮助,虽然有些事情更整洁,但做任何复杂的事情似乎都很痛苦!例如,我最初的想法是将顶级类别(根)绑定到树,并使用 HierarchicalDataTemplate 渲染所有实体:

                <sdk:TreeView Margin="0,3,30,3" ItemsSource="{Binding Categories}" Height="300">
                    <sdk:TreeView.ItemTemplate>
                        <sdk:HierarchicalDataTemplate ItemsSource="{Binding SubCategories}">
                            <StackPanel Orientation="Horizontal">
                                <CheckBox></CheckBox>
                                <TextBlock Text="{Binding Name}" HorizontalAlignment="Left"></TextBlock>
                            </StackPanel>
                        </sdk:HierarchicalDataTemplate>
                        <!---->
                    </sdk:TreeView.ItemTemplate>

问题是我无法将复选框绑定到任何内容,因为类别没有可以用来附加的字段它到一个产品...

请问 MVVM 专家能否对此进行一些说明,或者我应该简单地走“事件”路线?

非常感谢。

Sorry to ask just a wide question, but I am having trouble thinking how to approach this problem.

I have a many to many relationship:

Product 1 --- * ProductCategory * --- Category

Category also has:

Category 0..1 --- * Category (As SubCategories)

What I want to to see a tree view, with all categories, and be able to check a checkbox to create the ProductCategory entity. I do not how to approach this with MVVM.

enter image description here

In ASP.NET, when rendering the tree view I would check to see if the CategoryId of the node I was rendering was in my list of 'ProductCategory's if it was, I would check the box, and so setting up the initial state.

Then I would attach event handlers to the nodes (along with maybe some data) so that when the checkbox was checked/unchecked, it would add/remove the appropriate entity from my list.

The problem is I'm trying to move towards MVVM, and sometimes seeing how exactly it helps, and whilst some things a neater, doing anything complicated seems to be a pain! For example, my inital thought was to bind the top level Category (Root) to the tree, and render all entities using the HierarchicalDataTemplate:

                <sdk:TreeView Margin="0,3,30,3" ItemsSource="{Binding Categories}" Height="300">
                    <sdk:TreeView.ItemTemplate>
                        <sdk:HierarchicalDataTemplate ItemsSource="{Binding SubCategories}">
                            <StackPanel Orientation="Horizontal">
                                <CheckBox></CheckBox>
                                <TextBlock Text="{Binding Name}" HorizontalAlignment="Left"></TextBlock>
                            </StackPanel>
                        </sdk:HierarchicalDataTemplate>
                        <!---->
                    </sdk:TreeView.ItemTemplate>

The problem is that I cannot bind my checkbox to anything, because Category has no field I can use to attach it to a product...

Please can an MVVM guru throw some light on this, or should I simply go down the 'event' route?

Many Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-11-12 02:03:29

一种方法是将 IsChecked 属性添加到类别视图模型中,然后将其绑定到复选框。当它设置为 true 时,视图模型可以调用任何地方。例如,它可以将自己的产品名称添加到另一个控件的过滤器集合中。

mvvm 模式的优点之一是视图模型可以相互引用或相互引用,并且还具有您不想放入实际实体中的属性

One approach is to add a IsChecked property to you category viewmodel, then bind that to the checkbox. When its get set to true the viewmodel can make a call to where ever. It can for example add its own product name to a filter collection for another control.

One of the strong sides of the mvvm pattern is that the viewmodels can have references to each other or other things, and also have properties that you dont want to put in the actual entites

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文