WPF树形数据绑定模型&存储库

发布于 2024-09-03 11:10:29 字数 943 浏览 3 评论 0原文

我有一个定义明确的树存储库。我可以在其中重命名项目、向上、向下移动它们等。添加新项目和删除项目。

数据存储在表中,如下所示:

Index    Parent   Label    Left    Right
1        0        root     1       14
2        1        food     2       7
3        2        cake     3       4
4        2        pie      5       6
5        1        flowers  8       13
6        5        roses    9       10
7        5        violets  11      12

表示以下树:

                       (1) root (14)
         (2) food (7)                  (8) flowers (13)
  (3) cake (4)  (5) pie (6)    (9) roeses (10) (11) violets (12)

并直观地显示如下:

root
  food
    cake
    pie
  flowers
    roses
    violets

现在,我的问题是如何以可绑定的方式表示它,以便 TreeView 可以处理所有可能的数据更改? 重命名很容易,我所需要做的就是使标签成为可更新字段。但是如果用户将花朵移到食物上方怎么办?我可以进行相关数据更改,但它们会导致树中所有其他项目的完整数据更改。我发现的所有可绑定层次结构的示例都适用于非静态树。

所以我当前(也是不好的)解决方案是在重定位更改后重新加载显示的树。

任何方向都会好的。

谢谢

I have a well defined tree repository. Where I can rename items, move them up, down, etc. Add new and delete.

The data is stored in a table as follows:

Index    Parent   Label    Left    Right
1        0        root     1       14
2        1        food     2       7
3        2        cake     3       4
4        2        pie      5       6
5        1        flowers  8       13
6        5        roses    9       10
7        5        violets  11      12

Representing the following tree:

                       (1) root (14)
         (2) food (7)                  (8) flowers (13)
  (3) cake (4)  (5) pie (6)    (9) roeses (10) (11) violets (12)

and displayed visually as follows:

root
  food
    cake
    pie
  flowers
    roses
    violets

Now, my problem is how to represent this in a bindable way, so that a TreeView can handle all the possible data changes?
Renaming is easy, all I need is to make the label an updatble field. But what if a user moves flowers above food? I can make the relevant data changes, but they cause a complete data change to all other items in the tree. And all the examples I found of bindable hierarchies are good for non static trees..

So my current (and bad) solution is to reload the displayed tree after relocation change.

Any direction will be good.

Thanks

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

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

发布评论

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

评论(2

吃兔兔 2024-09-10 11:10:29

您是否尝试过使用相当简单的结构,包括 ObservableCollection<>对于子节点?实现该界面的东西:

interface ITreeNode<T>
    where T : ITreeNode
{
    string Name { get; set; }
    ObservableCollection<T> Children { get; }
}

然后您可以为 TreeView 的 XAML 使用 HeirarchicalDataTemplate:

<TreeView ItemsSource="{Binding MyTree}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

您必须自己进行拖放操作,但这应该可以让您开始显示所有内容。

我最近必须自己完成上述操作,这篇 CodeProject 文章对我有所帮助,以防您需要更多: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

更新:
由于您当前拥有表格格式的所有内容,并且我假设您不想修改默认数据结构,因此请使用 IValueConverter 来获取您想要的项目。此代码未经测试,但我会尝试类似于以下内容的操作:

{Binding Index, Converter={StaticResource myConverter}}

当您声明 myConverter 时,为其提供对表格数据的引用。使用索引作为值并引用表,您可以从 Convert 方法返回一个带有适当结果的 IEnumerable。

如果您需要更多详细信息,请告诉我。

Have you tried using a rather simple structure including an ObservableCollection<> for the child nodes? Something that would implement the interface:

interface ITreeNode<T>
    where T : ITreeNode
{
    string Name { get; set; }
    ObservableCollection<T> Children { get; }
}

Then you can use a HeirarchicalDataTemplate for your TreeView's XAML:

<TreeView ItemsSource="{Binding MyTree}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

You'll have to work out drag-and-drop yourself, but that should get you started to display everything.

I just recently had to do the above myself, and this CodeProject article helped me, in case you need any more: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

Update:
Since you currently have everything in a table format and I'll assume you won't want to revise your default data structure, use an IValueConverter to get the items you want. This code is untested, but I'd try something similar to the following:

{Binding Index, Converter={StaticResource myConverter}}

When you declare myConverter, give it a reference to your tabular data. Using the index as the value and having the reference to the table, you can return from the convert method an IEnumerable with the appropriate results.

Let me know if you need more detail.

风蛊 2024-09-10 11:10:29

不要使用 TreeView 使用第 3 方图形控件。通过 Google 进行简短搜索,找到了几个网站,例如 GoSilverlightDemo 查看 OrgChartEditor 示例,您可以拖动节点并更改链接。

Don't use a TreeView use a 3rd party graph control. A brief google search came up with several sites, check out this one for example GoSilverlightDemo check out the OrgChartEditor sample, you can drag nodes around and change the links.

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